Jedis-缓存操作具体实现代码。
来源:互联网 发布:看韩国电影软件 编辑:程序博客网 时间:2024/06/11 23:53
import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import redis.clients.jedis.JedisPool;import redis.clients.jedis.JedisPoolConfig;/** * Created by YeCongZhi on 2017/6/21. */@Configurationpublic class JedisConfiguation { Logger logger = LoggerFactory.getLogger(JedisConfiguation.class); @Value("${redisA.host:}")//IP private String hostA; @Value("${redisA.port:}")//端口 private int portA; @Value("${redisA.password:}")//密码 private String passwordA; @Value("${redisA.database:}")//数据库 private int databaseA; @Value("${redisB.host:}")//IP private String hostB; @Value("${redisB.port:}")//端口 private int portB; @Value("${redisB.password:}")//密码 private String passwordB; @Value("${redisB.database:}")//数据库 private int databaseB; @Value("${redis.maxTotal:}")//最大连接数 private int maxTotal; @Value("${redis.maxIdle:}")//最大空闲连接数 private int maxIdle; @Value("${redis.minIdle:}")//最小空闲连接数 private int minIdle; @Value("${redis.maxWaitMillis:}")//获取连接时的最大等待毫秒数 private Long maxWaitMillis; @Value("${redis.testOnBorrow:false}")//在获取连接的时候检查有效性 private boolean testOnBorrow; @Value("${redis.MinEvictableIdleTimeMillis:}")//多长时间后回收空闲连接 private Long MinEvictableIdleTimeMillis; @Bean("JedisPoolA") public JedisPool redisPoolFactoryA() {// logger.info("[redisA服务地址:]" + hostA);// logger.info("[redisA服务端口:]" + portA);// logger.info("[redisA密码:]" + passwordA); JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxIdle(maxIdle); poolCofig.setMinIdle(minIdle); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis); poolCofig.setTestOnBorrow(testOnBorrow); JedisPool jedisPool = new JedisPool(poolCofig, hostA, portA, 0, passwordA); return jedisPool; } @Bean("JedisPoolB") public JedisPool redisPoolFactoryB() {// logger.info("[redisB服务地址:]" + hostB);// logger.info("[redisB服务端口:]" + portB);// logger.info("[redisB密码:]" + passwordB); JedisPoolConfig poolCofig = new JedisPoolConfig(); poolCofig.setMaxTotal(maxTotal); poolCofig.setMaxIdle(maxIdle); poolCofig.setMinIdle(minIdle); poolCofig.setMaxWaitMillis(maxWaitMillis); poolCofig.setMinEvictableIdleTimeMillis(MinEvictableIdleTimeMillis); poolCofig.setTestOnBorrow(testOnBorrow); JedisPool jedisPool = new JedisPool(poolCofig, hostB, portB, 0, passwordB); return jedisPool; }}
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Service;import redis.clients.jedis.Jedis;import redis.clients.jedis.JedisPool;/** * Created by YeCongZhi on 2017/6/21. * 使用示例 @Autowired private JedisClientService jedisClientService; jedisClientService.setValueToA("AAAAA","TFDSFSDF333"); System.out.println(jedisClientService.getValueFromA("AAAAA")); jedisClientService.setValueToB("BBBBB","AAREGRTE33"); System.out.println(jedisClientService.getValueFromB("BBBBB")); */@Servicepublic class JedisClientService { @Autowired @Qualifier("JedisPoolA") private JedisPool jedisPoolA; @Autowired @Qualifier("JedisPoolB") private JedisPool jedisPoolB; /**获取key的value值**/ public synchronized String getValueFromA(String key) { /**获取jedis实例*/ Jedis jedis = jedisPoolA.getResource(); String str = ""; try { str = jedis.get(key); } finally { try { /**close方法 包含两个方法 * returnBrokenResource * returnResource*/ jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setValueToA(String key, String value) { /**获取jedis实例*/ Jedis jedis = jedisPoolA.getResource(); String str = ""; try { str = jedis.set(key, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setexValueToA(String key, int seconds, String value) { /**获取jedis实例*/ Jedis jedis = jedisPoolA.getResource(); String str = ""; try { str = jedis.setex(key, seconds, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**删除key**/ public synchronized Long delFromA(String key) { /**获取jedis实例*/ Jedis jedis = jedisPoolA.getResource(); Long cnt; try { cnt = jedis.del(key); } finally { try { /**close方法 包含两个方法 * returnBrokenResource * returnResource*/ jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return cnt; } /******************************************************************/ /**获取key的value值**/ public synchronized String getValueFromB(String key) { /**获取jedis实例*/ Jedis jedis = jedisPoolB.getResource(); String str = ""; try { str = jedis.get(key); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setValueToB(String key, String value) { /**获取jedis实例*/ Jedis jedis = jedisPoolB.getResource(); String str = ""; try { str = jedis.set(key, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**保存key - velue */ public synchronized String setexValueToB(String key, int seconds, String value) { /**获取jedis实例*/ Jedis jedis = jedisPoolB.getResource(); String str = ""; try { str = jedis.setex(key, seconds, value); } finally { try { jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return str; } /**删除key**/ public synchronized Long delFromB(String key) { /**获取jedis实例*/ Jedis jedis = jedisPoolB.getResource(); Long cnt; try { cnt = jedis.del(key); } finally { try { /**close方法 包含两个方法 * returnBrokenResource * returnResource*/ jedis.close(); } catch (Exception e) { e.printStackTrace(); } } return cnt; }}
阅读全文
0 0
- Jedis-缓存操作具体实现代码。
- Redis-RedisTemplate 缓存操作具体实现代码
- Jedis缓存实现类
- Jedis缓存实现类
- 用Jedis操作redis缓存
- Java文件读写操作具体代码实现
- Redis缓存系统(一)Java-Jedis操作Redis,基本操作以及 实现对象保存
- Redis缓存系统(1)Java-Jedis操作Redis,基本操作以及 实现对象保存
- jedis 操作redis (java 缓存机制)
- SpringBoot 集成Jedis操作Redis缓存
- Jedis客户端操作redis缓存命令详解
- 字符串操作函数的具体实现的代码
- Jdbc--具体代码实现
- jedis缓存
- Jedis 缓存
- jedis 连接池工具类具体实现demo。
- 安卓开发文件缓存方法的具体实现(参考代码)
- Jedis实现Redis操作事件通知
- 3d指针
- VS2010 winform中的文本框怎样设置为只读状态
- linux 3.4.10 内核内存管理源代码分析1:源代码阅读工具,编译及调试
- 分布式服务化系统一致性的“最佳实干”
- 8.15 F
- Jedis-缓存操作具体实现代码。
- C#操作word插件
- shell 的常用操作
- iOS Hacker 重签名实现无需越狱注入动态库 dylib
- 关于实习经历
- SharedPreferences保存各种类型的数据
- 树莓派安装与远程桌面控制
- 图的存储结构——邻接矩阵
- 一张图看懂volume backup流程