(spring-data-redis)SSM框架下使用redis作为mybatis的二级缓存

为了使查询更高效所以本文采用redis作为二级缓存

开发环境:maven3.5+eclipse4.5.2+jdk1.7+tomcat1.7

操作系统:win7 64

spring 4.2.4 mybatis 3.4.2  spring-data-redis 1.6.2 


redis如果没有linux服务器或者虚拟机(关于linux安装redis请查看文章)

可以下载一个window版 下载地址  https://github.com/MicrosoftArchive/redis/releases


设置redis密码

下载解压后。打开 redis.windows-service.conf 的文件

修改#requirepass foobared

requirepass 123456 及开启redis权限密码为123456


cmd命令到redis跟路径,redis-server.exe  redis.windows-service.conf 启动redis服务端

(spring-data-redis)SSM框架下使用redis作为mybatis的二级缓存_第1张图片

另外再打开一个cmd命令到redis跟路径,redis-cli.exe 启动redis客户端,auth 密码(123456) 表示赋了权限

就可以在cmd窗口进行redis的简单key value的操作了

(spring-data-redis)SSM框架下使用redis作为mybatis的二级缓存_第2张图片


回到正题。进入项目

在pom.xml 添加redis配置

1、添加jar依赖

2.6.2
1.6.2.RELEASE
		
			org.springframework.data
			spring-data-redis
			${spring-data-redis.version}
			
				
					log4j
					log4j
				
				
					slf4j-log4j12
					org.slf4j
				
			
		
		
			redis.clients
			jedis
			${jedis.verison}
		


2、spring配置


添加spring-redis.xml




	
	
	
	    
	    
	    
	
	
	   
	
	cn.erp.commons.cache.RedisCacheTransfer">
	    
	 


spring-dataSource.xml



		
		
		
            
                
                    
                        
                            dialect=mysql
                        
                    
                
            
        
        
        
            
                true
                
                false
                
                true
                
                true
                
                true
                
                true
                
                FULL
                
                BATCH
                
                25000
            
       

3、创建对应的创建RedisCache类

/**
 * 使用第三方内存数据库Redis作为二级缓存
 * @author leeclick
 * 2017-10-02 15:00:36
 */
public class RedisCache implements Cache
{
    private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

    private static JedisConnectionFactory jedisConnectionFactory;

    private final String id;

    /**
     * The {@code ReadWriteLock}.
     */
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();

    public RedisCache(final String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        logger.debug("MybatisRedisCache:id=" + id);
        this.id = id;
    }

    @Override
    public void clear()
    {
        JedisConnection connection = null;
        try
        {
            connection = jedisConnectionFactory.getConnection();
            connection.flushDb();
            connection.flushAll();
        }
        catch (JedisConnectionException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (connection != null) {
                connection.close();
            }
        }
    }

    @Override
    public String getId()
    {
        return this.id;
    }

    @Override
    public Object getObject(Object key)
    {
        Object result = null;
        JedisConnection connection = null;
        try
        {
            connection = jedisConnectionFactory.getConnection();
            RedisSerializer serializer = new JdkSerializationRedisSerializer();
            result = serializer.deserialize(connection.get(serializer.serialize(key)));
        }
        catch (JedisConnectionException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (connection != null) {
                connection.close();
            }
        }
        return result;
    }

    @Override
    public ReadWriteLock getReadWriteLock()
    {
        return this.readWriteLock;
    }

    @Override
    public int getSize()
    {
        int result = 0;
        JedisConnection connection = null;
        try
        {
            connection = jedisConnectionFactory.getConnection();
            result = Integer.valueOf(connection.dbSize().toString());
        }
        catch (JedisConnectionException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (connection != null) {
                connection.close();
            }
        }
        return result;
    }

    @Override
    public void putObject(Object key, Object value)
    {
        JedisConnection connection = null;
        try
        {
            connection = jedisConnectionFactory.getConnection();
            RedisSerializer serializer = new JdkSerializationRedisSerializer();
            connection.set(serializer.serialize(key), serializer.serialize(value));
        }
        catch (JedisConnectionException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (connection != null) {
                connection.close();
            }
        }
    }

    @Override
    public Object removeObject(Object key)
    {
        JedisConnection connection = null;
        Object result = null;
        try
        {
            connection = jedisConnectionFactory.getConnection();
            RedisSerializer serializer = new JdkSerializationRedisSerializer();
            result =connection.expire(serializer.serialize(key), 0);
        }
        catch (JedisConnectionException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (connection != null) {
                connection.close();
            }
        }
        return result;
    }

    public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
        RedisCache.jedisConnectionFactory = jedisConnectionFactory;
    }

} 
  
创建RedisCacheTransfer类

/**
 * 
 * 静态注入中间类
 * @author leeclick
 * 2017-10-02 15:02:44
 */
public class RedisCacheTransfer 
{

    @Autowired
    public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
        RedisCache.setJedisConnectionFactory(jedisConnectionFactory);
    }

}

4、在需要缓存的Mapper.xml配置文件中开启二级缓存




 		
		

在页面上查询一次后

在redis客户端redis-cli.exe 输入keys * 

发现查询结果已经存入redis

(spring-data-redis)SSM框架下使用redis作为mybatis的二级缓存_第3张图片



你可能感兴趣的:(redis)