• Redis工具类


      作为一个重要的数据缓存机制,redis有着广泛的运用。下面是一个redis工具类。  

    import com.google.gson.Gson;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPoolConfig;
    import redis.clients.jedis.JedisSentinelPool;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    /**
     * redis工具类
     */
    public class UtilRedis {
        private static JedisSentinelPool jedisSentinelPool;
        static {
            // redis 属性配置 start
            JedisPoolConfig config = new JedisPoolConfig();
            config.setMaxTotal(5000);
            config.setMaxIdle(256);
            config.setMaxWaitMillis(5000L);
            config.setTestOnBorrow(true);
            config.setTestOnReturn(true);
            config.setTestWhileIdle(true);
            config.setMinEvictableIdleTimeMillis(60000L);
            config.setTimeBetweenEvictionRunsMillis(3000L);
            config.setNumTestsPerEvictionRun(-1);
            // redis 属性配置 end
    
            Set<String> sentinels = new HashSet<String>();
            sentinels.add("127.0.0.1:26379"); // 此处放置ip及端口为 sentinel
            sentinels.add("127.0.0.1:27379"); // 此处放置ip及端口为 sentinel
            // 服务地址,如果有多个sentinel 则逐一add即可
            jedisSentinelPool = new JedisSentinelPool("mymaster", sentinels, config);
        }
    
        public static Jedis getJedis() {
            try {
                return jedisSentinelPool.getResource();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static void release(Jedis jedis) {
    //        jedis.close();
    
            try {
                jedisSentinelPool.returnResourceObject(jedis);
            } catch (Exception e) {
                e.printStackTrace();
    
                if (jedis.isConnected()) {
                    jedis.disconnect();
                    jedis.close();
                    jedis = null;
                    System.gc();
                }
            }
        }
    
    //    public static void main(String[] args) {
    //        Jedis jedis = UtilRedis.getJedis();
    //        jedis.set("datajava1","xxxx1");
    //        jedis.lpush("datalist1","a","b","c","d");
    //
    //        System.out.println(jedis.get("datajava1"));
    //        System.out.println(jedis.lrange("datalist1",0,-1));
    //
    //        UtilRedis.release(jedis);
    //
    //        System.out.println("-----------------------------------------");
    //
    //        Jedis jedis2 = UtilRedis.getJedis();
    //        jedis2.set("datajava2","xxxx2");
    //        jedis2.lpush("datalist2","a2","b2","c2","d2");
    //
    //        System.out.println(jedis2.get("datajava2"));
    //        System.out.println(jedis2.lrange("datalist2",0,-1));
    //
    //        UtilRedis.release(jedis2);
    //    }
        //将json字符串解析为相应类型的对象
        public static <T> List<T> convertObjs(List<String> jsons, Class<T> clazz) {
            Gson gson = new Gson();
            List<T> objs = new ArrayList<>();
            for (String json : jsons) {
                objs.add(gson.fromJson(json, clazz));
            }
            return objs;
        }
    }  
  • 相关阅读:
    Linq与Lambda,神一般的工作效率
    svn和git孰优孰劣
    关于C++的***5的输出问题
    POJ 3469 Dual Core CPU(最小割)
    HDU 4259 Double Dealing
    最大流Dinic算法
    HDU 4442 Physical Examination(2012年金华赛区现场赛A题)
    int ,long , long long类型的范围
    POJ 1679 The Unique MST(判断最小生成树是否唯一)
    HDU 4280 Island Transport(网络流)
  • 原文地址:https://www.cnblogs.com/lizhangyong/p/8625680.html
Copyright © 2020-2023  润新知