标签:object sts trace str byte ons eric 个数 interrupt
需要使用如下jar包
<!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool --> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.6</version> </dependency>
1.建立连接池
package com.spring.wzy; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; public class JedisPoolUtil { /** * 双重锁形式的单例 * */ private static volatile JedisPool jedisPool = null; private JedisPoolUtil(){} public static JedisPool getJedisPoolInstance(){ if(null == jedisPool){ synchronized (JedisPoolUtil.class) { if(null == jedisPool){ // GenericObjectPoolConfig poolConfig = new JedisPoolConfig(); JedisPoolConfig poolConfig = new JedisPoolConfig(); // poolConfig.setMaxActive(1000);//最大实例数 poolConfig.setMaxWaitMillis(100*1000);//最大等待时间 poolConfig.setMaxIdle(50);//最多空闲实例 poolConfig.setTestOnBorrow(true);//获得一个实例时检查连接可用性 jedisPool = new JedisPool(poolConfig, "192.168.23.128", 6379); } } } return jedisPool; } public static void release(JedisPool jedisPool, Jedis jedis){ if(null != jedis){ //jedis.close(); jedisPool.returnResourceObject(jedis); } } }
2.使用实例
public static void main(String[] args) throws InterruptedException { JedisPool jedisPool = JedisPoolUtil.getJedisPoolInstance(); Jedis j = null; try{ j = jedisPool.getResource(); // j.select(0);//选中数据库,默认是0号库 // j.flushAll();//清空数据库 // j.del("key"); //删除key // j.set("k1", "v1"); // j.set("k2", "v2"); // j.set("k2", "v3");//新值会把旧值覆盖 // System.out.println(j.exists("k2"));//返回ture/false // System.out.println(j.exists("k1","k3"));//返回存在的个数 // System.out.println(new String(j.get("k1".getBytes()))); // System.out.println(j.get("k2")); // Set<String> keys = j.keys("*"); // for(String k:keys){ // System.out.println(k); // } // j.move("k1", 2);//把k1剪切到2号库 // System.out.println(j.expire("k2", 10));//设置key的存活时间(s),执行成功返回1,否则返回0 // Thread.sleep(3000); // System.out.println(j.ttl("k2"));//查看key的过期时间 }catch (Exception e) { e.printStackTrace(); }finally{ JedisPoolUtil.release(jedisPool, j); }
标签:object sts trace str byte ons eric 个数 interrupt
原文地址:http://www.cnblogs.com/wwzyy/p/6057505.html