权限之Redis分布式缓存引入与保存缓存功能

对我们的权限做一些缓存。做缓存需要缓存的容器,故引入Redis(是特别流行的缓存容器)

说明:在配置文件中配置bean,其实就是在Spring容器中注入类,这个类在Spring容器中有唯一一个表示id,我们可以通过Spring容器去管理这个类,非常方便(类似ios中的ARC自动释放池);

第一步:引入Redis的配置(redis.xml和redis.properties)

redis.properties:


redis.host=127.0.0.1
redis.port=6379
redis.timeout=3000

redis.xml:



    
    
    
    
    
    
    
        
        
        
        
            
                
                    
                    
                    
                    
                    
                    
                
            
        
    

第二步:在pom.xml映入redis的依赖

    
    
      redis.clients
      jedis
      2.8.1
      jar
    

第三步:书写RedisPool类

package com.mall.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import javax.annotation.Resource;

@Service("redisPool")
@Slf4j
public class RedisPool {

    @Resource(name="shardedJedisPool")
    private ShardedJedisPool shardedJedisPool;

    public ShardedJedis instance(){
        return shardedJedisPool.getResource();
    }
    /**
     * 进行关闭
     * @param shardedJedis
     */
    public void safeClose(ShardedJedis shardedJedis){
        try {
            if(shardedJedis!=null){
                shardedJedis.close();
            }
        }catch (Exception e){
            //返还redis资源的时候出现异常
            log.error("return redis resource exception",e);
        }
    }
}

第四步:对Redis的封装

package com.mall.service;

import com.google.common.base.Joiner;
import com.mall.beans.CacheKeyConstans;
import com.mall.common.RedisPool;
import com.mall.util.JsonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import javax.annotation.Resource;

@Service
@Slf4j
public class SysCacheService {
    //引入redisPool
    @Resource(name="redisPool")
    private RedisPool redisPool;

    public void saveCache(String toSavedValue, int timeoutSeconds, CacheKeyConstans prefix){
        saveCache(toSavedValue,timeoutSeconds,prefix,null);
    }
    //方法的重载,keys的API;保存缓存
    public void saveCache(String toSavedValue, int timeoutSeconds, CacheKeyConstans prefix,String... keys ){
        if(toSavedValue==null){
            return;
        }
        ShardedJedis shardedJedis=null;
        try{
            //生成对应的cacheKey
            String cacheKey=generateCacheKey(prefix,keys);
            //拿到资源
            shardedJedis=redisPool.instance();
            shardedJedis.setex(cacheKey,timeoutSeconds,toSavedValue);
        }catch (Exception e){
            log.error("save cache exception,prefix:{},keys:{}",prefix.name(), JsonMapper.obj2String(keys),e);
        }finally {
            redisPool.safeClose(shardedJedis);
        }
    }
    //得到表格缓存
    public String getFromCache(CacheKeyConstans prefix,String... keys){
        ShardedJedis shardedJedis=null;
        String cacheKey=generateCacheKey(prefix,keys);
        try{
            shardedJedis=redisPool.instance();
            String value=shardedJedis.get(cacheKey);
            return value;
        }catch (Exception e){
            log.error("get from cache exception,prefix:{}.keys:{}",prefix.name(), JsonMapper.obj2String(keys),e);
            return null;
        }finally {
            redisPool.safeClose(shardedJedis);
        }

    }
    private String generateCacheKey(CacheKeyConstans prefix,String... keys){
        String key=prefix.name();
        if(key!=null&&keys.length>0){
            key+="_"+ Joiner.on("_").join(keys);
        }
        return key;
    }
}

 

 

 

你可能感兴趣的:(java)