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;
        }
    }
  • 相关阅读:
    【WPF】绘制柱状图、折线图、扇形图
    【WPF】获取容器宽高
    【SQL】SQL Server、MySQL、SQLite获取自增ID、自增ID清零
    【SQL Server操作】SQL Server重命名数据库及解决数据库无法分离的问题
    【数据迁移】MySQL数据库迁移到SQL Server
    【MySQL操作】MySQL导入导出数据库
    【MySQL操作】MySQL Workbench远程连接的一些操作
    【c#】遍历获得一个类的所有属性名
    vue 跨域 springCloud @CrossOrigin注解
    解决IDEA右侧maven不显示方法
  • 原文地址:https://www.cnblogs.com/Gyoung/p/5909201.html
Copyright © 2011-2022 走看看