版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/jiangtao_st/article/details/37699473

源码下载: http://download.csdn.net/detail/jiangtao_st/7623113

1、Maven配置

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency></span>

2、Properties 配置文件

  redis.pool.maxActive=100

  redis.pool.maxIdle=20

  redis.pool.maxWait=3000

  redis.ip=localhost

  redis.port=6379

3、代码详细实现的Client

/**
*
* <p>
* Redisclient訪问
* </p>
*
* @author 卓轩
* @创建时间:2014年7月11日
* @version: V1.0
*/
public class RedisClient { public static JedisPool jedisPool; // 池化管理jedis链接池 static { //读取相关的配置
ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait")); String ip = resourceBundle.getString("redis.ip");
int port = Integer.parseInt(resourceBundle.getString("redis.port")); JedisPoolConfig config = new JedisPoolConfig();
//设置最大连接数
config.setMaxTotal(maxActive);
//设置最大空暇数
config.setMaxIdle(maxIdle);
//设置超时时间
config.setMaxWaitMillis(maxWait); //初始化连接池
jedisPool = new JedisPool(config, ip, port);
} /**
* 向缓存中设置字符串内容
* @param key key
* @param value value
* @return
* @throws Exception
*/
public static boolean set(String key,String value) throws Exception{
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
} /**
* 向缓存中设置对象
* @param key
* @param value
* @return
*/
public static boolean set(String key,Object value){
Jedis jedis = null;
try {
String objectJson = JSON.toJSONString(value);
jedis = jedisPool.getResource();
jedis.set(key, objectJson);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
} /**
* 删除缓存中得对象,依据key
* @param key
* @return
*/
public static boolean del(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.del(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
} /**
* 依据key 获取内容
* @param key
* @return
*/
public static Object get(String key){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
Object value = jedis.get(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
jedisPool.returnResource(jedis);
}
} /**
* 依据key 获取对象
* @param key
* @return
*/
public static <T> T get(String key,Class<T> clazz){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
String value = jedis.get(key);
return JSON.parseObject(value, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
jedisPool.returnResource(jedis);
}
} }

4、Sharding 分片管理

/**
*
* <p>
* Sharding Redis Client 工具类
* </p>
*
* @author 卓轩
* @创建时间:2014年7月11日
* @version: V1.0
*/
public class ShardingRedisClient { private static ShardedJedisPool shardedJedisPool; static {
// 读取相关的配置
ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait")); String ip = resourceBundle.getString("redis.ip");
int port = Integer.parseInt(resourceBundle.getString("redis.port")); //设置配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait); //设置分片元素信息
JedisShardInfo shardInfo1 = new JedisShardInfo(ip,port);
JedisShardInfo shardInfo2 = new JedisShardInfo(ip,port);
List<JedisShardInfo> list = new ArrayList<JedisShardInfo>();
list.add(shardInfo1);
list.add(shardInfo2);
shardedJedisPool = new ShardedJedisPool(config, list);
} /**
* 向缓存中设置字符串内容
* @param key key
* @param value value
* @return
* @throws Exception
*/
public static boolean set(String key,String value) throws Exception{
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
jedis.set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
shardedJedisPool.returnResource(jedis);
}
} /**
* 向缓存中设置对象
* @param key
* @param value
* @return
*/
public static boolean set(String key,Object value){
ShardedJedis jedis = null;
try {
String objectJson = JSON.toJSONString(value);
jedis = shardedJedisPool.getResource();
jedis.set(key, objectJson);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
shardedJedisPool.returnResource(jedis);
}
} /**
* 删除缓存中得对象,依据key
* @param key
* @return
*/
public static boolean del(String key){
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
jedis.del(key);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
shardedJedisPool.returnResource(jedis);
}
} /**
* 依据key 获取内容
* @param key
* @return
*/
public static Object get(String key){
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
Object value = jedis.get(key);
return value;
} catch (Exception e) {
e.printStackTrace();
return false;
}finally{
shardedJedisPool.returnResource(jedis);
}
} /**
* 依据key 获取对象
* @param key
* @return
*/
public static <T> T get(String key,Class<T> clazz){
ShardedJedis jedis = null;
try {
jedis = shardedJedisPool.getResource();
String value = jedis.get(key);
return JSON.parseObject(value, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
shardedJedisPool.returnResource(jedis);
}
} }

5、 单元測试、保存对象、写入对象

/**
*
* <p>
* 測试独立redis client
* </p>
*
* @author 卓轩
* @创建时间:2014年7月11日
* @version: V1.0
*/
public class SimpleClient { @Test
public void userCache(){ //向缓存中保存对象
UserDO zhuoxuan = new UserDO();
zhuoxuan.setUserId(113445);
zhuoxuan.setSex(1);
zhuoxuan.setUname("卓轩");
zhuoxuan.setUnick("zhuoxuan");
zhuoxuan.setEmail("zhuoxuan@mogujie.com");
//调用方法处理
boolean reusltCache = RedisClient.set("zhuoxuan", zhuoxuan);
if (reusltCache) {
System.out.println("向缓存中保存对象成功。");
}else{
System.out.println("向缓存中保存对象失败。 ");
}
} @Test
public void getUserInfo(){ UserDO zhuoxuan = RedisClient.get("zhuoxuan",UserDO.class);
if(zhuoxuan != null){
System.out.println("从缓存中获取的对象。" + zhuoxuan.getUname() + "@" + zhuoxuan.getEmail());
} } }

最新文章

  1. &lt;三&gt;JDBC_面向对象思想的体现
  2. Theano3.2-练习之数据集及目标函数介绍
  3. MFC 实现字符串的移动
  4. 第一章 Spring Security是什么?
  5. 023医疗项目-模块二:药品目录的导入导出-从数据库中查出数据用XSSF导出excel并存放在虚拟目录最后下载(包括调试)
  6. 关于查询oracle in &gt;1000 的讨论
  7. hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)
  8. [cc150] check palindrome of a singly linked list
  9. 新秀nginx源代码分析数据结构篇(四)红黑树ngx_rbtree_t
  10. 【Swift】IOS开发中自定义转场动画
  11. Redis学习笔记(1)——Redis简介
  12. 【网址】PHP参考文档
  13. HTML和CSS总结
  14. 标签页QTabWidget
  15. centos上shellcheck的安装
  16. Chrome浏览器F12开发者工具的几个小技巧总结
  17. Fibonacci数列的两种实现方式
  18. PHP的extension_dir设置问题
  19. Fiddler 502问题
  20. kaggle竞赛

热门文章

  1. mysql数据库连接出问题,提示超时 java.sql.SQLException: An attempt by a client to checkout a Connection has timed out.解决办法
  2. docker-compose 手工指定容器IP
  3. image magick 批量转换文件
  4. C#退出程序方法分类
  5. 『Python CoolBook』数据结构和算法_字典比较&amp;字典和集合
  6. 『TensorFlow』函数查询列表_神经网络相关
  7. mysql数据库的基础操作
  8. 关于window 图片系统功能
  9. 获取页面定位元素left top
  10. vuex-getter