redis工具类
2018-07-20 来源:open-open
pom.xml
<!-- redis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version> <type>jar</type> </dependency>
redis.propertis
#\u6700\u5927\u5206\u914d\u7684\u5bf9\u8c61\u6570 redis.pool.maxActive=1024 #\u6700\u5927\u80fd\u591f\u4fdd\u6301idel\u72b6\u6001\u7684\u5bf9\u8c61\u6570 redis.pool.maxIdle=200 #\u5f53\u6c60\u5185\u6ca1\u6709\u8fd4\u56de\u5bf9\u8c61\u65f6\uff0c\u6700\u5927\u7b49\u5f85\u65f6\u95f4 redis.pool.maxWait=10000 #\u5f53\u8c03\u7528borrow Object\u65b9\u6cd5\u65f6\uff0c\u662f\u5426\u8fdb\u884c\u6709\u6548\u6027\u68c0\u67e5 redis.pool.testOnBorrow=true #\u5f53\u8c03\u7528return Object\u65b9\u6cd5\u65f6\uff0c\u662f\u5426\u8fdb\u884c\u6709\u6548\u6027\u68c0\u67e5 redis.pool.testOnReturn=true redis.pool.password=3.1414926 #IP redis.ip=192.168.0.12 #Port redis.port=6379
java:
package com.ibm.common.util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; /** * @author 加内特 */ public class PropUtils { protected static final String REDIS_RESOURCES_PROPERTIES_FILE_NAME = "redis.properties"; /** * 取得所有属性值 * * @param propertiesFileName * @return * @throws IOException */ protected static Properties getProperties(String propertiesFileName) throws IOException { Properties prop = new Properties(); InputStream is = PropUtils.class.getClassLoader().getResourceAsStream(propertiesFileName); try { prop.load(is); } catch (IOException e) { throw e; } return prop; } /** * 取得redis文件 * * @return * @throws IOException */ public static Properties getRedisResourcesProperties() { try { return getProperties(REDIS_RESOURCES_PROPERTIES_FILE_NAME); } catch (IOException ioe) { ioe.printStackTrace(); } return null; } /** * 取得redis文件的属性值 * * @return * @throws IOException */ public static String getRedisValue(String key) { Properties properties = getRedisResourcesProperties(); return properties.getProperty(key); } }
package com.ibm.common.cache; import com.ibm.common.util.PropUtils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; /** * Redis操作接口 * * @author 加内特 */ public class RedisUtils { private static JedisPool pool = null; private static ThreadLocal<JedisPool> poolThreadLocal = new ThreadLocal<JedisPool>(); /** * 构建redis连接池 * * @param ip * @param port * @return JedisPool */ public static JedisPool getPool() { if (pool == null) { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxActive(Integer.valueOf(PropUtils.getRedisValue("redis.pool.maxActive"))); config.setMaxIdle(Integer.valueOf(PropUtils.getRedisValue("redis.pool.maxIdle"))); config.setMaxWait(Long.valueOf(PropUtils.getRedisValue("redis.pool.maxWait"))); config.setTestOnBorrow(Boolean.valueOf(PropUtils.getRedisValue("redis.pool.testOnBorrow"))); config.setTestOnReturn(Boolean.valueOf(PropUtils.getRedisValue("redis.pool.testOnReturn"))); // 测试环境 // pool = new JedisPool(config, bundle.getString("redis.ip"), // Integer.valueOf(bundle.getString("redis.port"))); // 线上环境 pool = new JedisPool(config, PropUtils.getRedisValue("redis.ip"), Integer.valueOf(PropUtils.getRedisValue("redis.port")), 100000, PropUtils.getRedisValue("redis.pool.password")); } return pool; } public static JedisPool getConnection() { // ②如果poolThreadLocal没有本线程对应的JedisPool创建一个新的JedisPool,将其保存到线程本地变量中。 if (poolThreadLocal.get() == null) { pool = RedisUtils.getPool(); poolThreadLocal.set(pool); return pool; } else { return poolThreadLocal.get();// ③直接返回线程本地变量 } } /** * 返还到连接池 * * @param pool * @param redis */ public static void returnResource(JedisPool pool, Jedis redis) { if (redis != null) { pool.returnResource(redis); } } /** * 获取数据 * * @param key * @return */ public static String get(String key) { String value = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return value; } /** * 获取数据 * * @param key * @return */ public static byte[] get(byte[] key) { byte[] value = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); value = jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return value; } /** * 删除数据 * * @param key * @return */ public static Long del(String key) { Long value = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); value = jedis.del(key); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return value; } /** * 删除数据 * * @param key * @return */ public static Long del(byte[] key) { Long value = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); value = jedis.del(key); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return value; } /** * 判断是否存在 * * @param key * @return */ public static Boolean exists(String key) { Boolean value = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); value = jedis.exists(key); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return value; } /** * 赋值数据 * * @param key * @param value * @param expireSeconds(过期时间,秒) * @return value */ public static Long set(String key, String value, int expireSeconds) { Long result = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); jedis.set(key, value); result = jedis.expire(key, expireSeconds); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return result; } /** * 设置过期时间 * * @param key * @param expireSeconds(过期时间,秒) * @return value */ public static Long expire(String key, int expireSeconds) { Long result = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); result = jedis.expire(key, expireSeconds); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return result; } /** * 赋值数据 * * @param key * @return */ public static String set(String key, String value) { String result = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); result = jedis.set(key, value); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return result; } /** * 赋值数据 * * @param key * @return */ public static Long sadd(String key, String value) { Long result = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); result = jedis.sadd(key, value); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return result; } /** * 判断set中是否有值 * * @param key * @return */ public static Boolean sismember(String key, String member) { Boolean result = null; JedisPool pool = null; Jedis jedis = null; try { pool = getPool(); jedis = pool.getResource(); result = jedis.sismember(key, member); } catch (Exception e) { e.printStackTrace(); } finally { // 释放redis对象 pool.returnBrokenResource(jedis); // 返还到连接池 returnResource(pool, jedis); } return result; } }
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
上一篇:Java定期自动截屏的代码
下一篇:文件排序Java工具类
最新资讯
热门推荐