博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ssm+redis整合之redis连接池注入
阅读量:5029 次
发布时间:2019-06-12

本文共 4034 字,大约阅读时间需要 13 分钟。

package com.tp.soft.redis;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;public class RedisCacheTransfer {        @Autowired    public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {        RedisCacheAn.setJedisConnectionFactory(jedisConnectionFactory);    }}

 

spring-redis注入

 

二级缓存引入类

package com.tp.soft.redis;import java.util.concurrent.locks.ReadWriteLock;import java.util.concurrent.locks.ReentrantReadWriteLock;import org.apache.ibatis.cache.Cache;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.connection.jedis.JedisConnection;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializer;/* * 使用第三方缓存服务器,处理二级缓存 */public class RedisCacheAn implements Cache {    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();    private String id;        private static JedisConnectionFactory jedisConnectionFactory;        public RedisCacheAn(final String id) {        if (id == null) {            throw new IllegalArgumentException("Cache instances require an ID");        }        this.id = id;    }    public String getId() {        return this.id;    }    public void putObject(Object key, Object value) {        JedisConnection conn = null;        try{            conn = jedisConnectionFactory.getConnection();            RedisSerializer serializer = new JdkSerializationRedisSerializer();            conn.set(serializer.serialize(key), serializer.serialize(value));        }catch (Exception e) {            e.printStackTrace();        }finally{            if(conn != null){                conn.close();            }        }    }    public Object getObject(Object key) {        JedisConnection conn = null;        Object res = null;        try{            conn = jedisConnectionFactory.getConnection();            RedisSerializer serializer = new JdkSerializationRedisSerializer();            byte[] bs = conn.get(serializer.serialize(key));            res = serializer.deserialize(bs);        }catch (Exception e) {            e.printStackTrace();        }finally{            if(conn != null){                conn.close();            }        }        return res;    }    public Object removeObject(Object key) {        JedisConnection conn = null;        Object res = null;        try{            conn = jedisConnectionFactory.getConnection();            RedisSerializer serializer = new JdkSerializationRedisSerializer();            conn.expire(serializer.serialize(key), 0);        }catch (Exception e) {            e.printStackTrace();        }finally{            if(conn != null){                conn.close();            }        }        return res;    }    public void clear() {        JedisConnection conn = null;        try{            conn = jedisConnectionFactory.getConnection();            conn.flushDb();            conn.flushAll();        }catch (Exception e) {            e.printStackTrace();        }finally{            if(conn != null){                conn.close();            }        }    }    public int getSize() {        JedisConnection conn = null;        int res = 0;        try{            conn = jedisConnectionFactory.getConnection();            res = Integer.parseInt(conn.dbSize().toString());        }catch (Exception e) {            e.printStackTrace();        }finally{            if(conn != null){                conn.close();            }        }        return res;    }    public ReadWriteLock getReadWriteLock() {        return readWriteLock;    }    public static void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {        RedisCacheAn.jedisConnectionFactory = jedisConnectionFactory;    }}

 

转载于:https://www.cnblogs.com/tplovejava/p/7169138.html

你可能感兴趣的文章
微信小程序wx:key以及wx:key=" *this"详解:
查看>>
下拉框比较符
查看>>
2.2.5 因子的使用
查看>>
css选择器
查看>>
photoplus
查看>>
Python 拓展之推导式
查看>>
[Leetcode] DP-- 474. Ones and Zeroes
查看>>
80X86寄存器详解<转载>
查看>>
c# aop讲解
查看>>
iterable与iterator
查看>>
返回顶部(动画)
查看>>
webpack+react+antd 单页面应用实例
查看>>
Confluence 6 SQL Server 数据库驱动修改
查看>>
Confluence 6 通过 SSL 或 HTTPS 运行 - 备注和问题解决
查看>>
【47.76%】【Round #380B】Spotlights
查看>>
Git(使用码云)
查看>>
分享Java web 开发必游之路
查看>>
IIS初始化(预加载),解决第一次访问慢,程序池被回收问题(转载)
查看>>
Bean的Scope
查看>>
【BZOJ】3142: [Hnoi2013]数列
查看>>