zoukankan      html  css  js  c++  java
  • ssm+redis整合之redis连接池注入

    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注入

    <bean id="jedisConnectionFactory" class="com.tp.soft.redis.RedisCacheTransfer">
            <property name="jedisConnectionFactory" ref="connectionFactory"></property>
        </bean>

    二级缓存引入类

    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<Object> 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<Object> 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<Object> 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;
        }
    
    
    }
  • 相关阅读:
    object detection物体检测基本概念
    anaconda python环境搭建
    Eclipse环境下添加package到工程的classpath
    34-使用函数实现-文件拷贝
    33-使用函数实现-斐波那契数列
    32-简单的位置参数
    31-分段进行文件拷贝
    30-Python文件拷贝
    29-简单的文件对象基础操作
    28-石头剪刀布:三局两胜
  • 原文地址:https://www.cnblogs.com/tplovejava/p/7169138.html
Copyright © 2011-2022 走看看