zoukankan      html  css  js  c++  java
  • java 简单使用redis

    1.配置文件

      <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxTotal" value="50" />
            <property name="maxIdle" value="8" />
            <property name="maxWaitMillis" value="1000" />
            <property name="testOnBorrow" value="true"/>
            <property name="testOnReturn" value="true"/>
        </bean>
    
        <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
            <constructor-arg index="0" ref="jedisPoolConfig" />
            <constructor-arg index="1" value="192.168.1.76" type="java.lang.String"/>
            <constructor-arg index="2" value="6379"  type="int"/>
        </bean>

    2.复杂对象,通过序列化成进进制存储到redis中

    @Repository("RedisCacheImpl")
    public class RedisCacheImpl implements IRedisCache {
    
        private static final Logger logger = LoggerFactory.getLogger(RedisCacheImpl.class);
    
        @Autowired(required = false)
        protected JedisPool pool;
    
        @Override
        public void put(String key, Object value) {
            Jedis jedis = null;
            try {
                jedis = pool.getResource();
                jedis.set(key.getBytes(), SerializeUtil.serialize(value));
            } catch (Exception e) {
                logger.error("redis error:", e);
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
        }
    
        @SuppressWarnings({"unchecked"})
        @Override
        public <T> T get(String key) {
            Jedis jedis = null;
            try {
                jedis = pool.getResource();
                byte[] value = jedis.get(key.getBytes());
                if (value == null) {
                    return null;
                }
                return (T) SerializeUtil.unSerialize(value);
            } catch (Exception e) {
                logger.error("redis error:", e);
                return null;
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
        }
    
        @SuppressWarnings({"unchecked"})
        @Override
        public boolean del(String key) {
            Jedis jedis = null;
            try {
                jedis = pool.getResource();
                return jedis.del(key.getBytes()) > 0;
            } catch (Exception e) {
                logger.error("redis error:", e);
                return false;
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
        }
    }

    3.序列化类

    public class SerializeUtil {
    
        private static final Logger logger = LoggerFactory.getLogger(SerializeUtil.class);
    
    
        public static byte[] serialize(Object object) {
            ObjectOutputStream oos = null;
            ByteArrayOutputStream baos = null;
            try {
                // 序列化
                baos = new ByteArrayOutputStream();
                oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                byte[] bytes = baos.toByteArray();
                return bytes;
            } catch (Exception e) {
                logger.error("serializer error:", e);
            }
            return null;
        }
    
        public static Object unSerialize(byte[] bytes) {
            ByteArrayInputStream bais = null;
            try {
                // 反序列化
                bais = new ByteArrayInputStream(bytes);
                ObjectInputStream ois = new ObjectInputStream(bais);
                return ois.readObject();
            } catch (Exception e) {
                logger.error("serializer error:", e);
            }
            return null;
        }
    }
  • 相关阅读:
    第二次冲刺阶段第四天
    第二次冲刺阶段第三天
    第二次冲刺阶段第二天
    人月神话阅读笔记03
    第二次冲刺阶段第一天
    学习进度条(十二)
    课堂练习-找水王
    学习进度条(十一)
    学习进度表第十周
    构建之法阅读笔记06
  • 原文地址:https://www.cnblogs.com/Gyoung/p/5909201.html
Copyright © 2011-2022 走看看