源代码下载:   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>
* Redis客户端访问
* </p>
*
* @author 卓轩
* @创建时间:2014年7月11日
* @version: V1.0
*/
public class RedisClient { public static JedisPool jedisPool; 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、 单元测试、保存对象、写入对象

/**
*
* <p>
* 测试独立redis 客户端
* </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());
} } }

Redis缓存系统-Java-Jedis操作Redis,基本操作以及 实现对象保存的更多相关文章

  1. Redis【4】Java Jedis 操作 Redis~

    package redis.redis; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; /** * 描 ...

  2. redis缓存数据库及Python操作redis

    缓存数据库介绍  NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库,随着互联网web2.0网站的兴起,传统的关系数据库在应付web2.0网站, 特 ...

  3. Redis(二)Jedis操作Redis

    如果测试连接的时候,报下面这个异常,可以参考下面的博客进行处理: Exception in thread "main" redis.clients.jedis.exceptions ...

  4. Redis入门和Java利用jedis操作redis

    Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...

  5. JAVA中通过Jedis操作Redis连接与插入简单库

    一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...

  6. Java中Jedis操作Redis与Spring的整合

    Redis是一个key-value存储系统.它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类型都支持push/pop. ...

  7. jedis操作redis的几种常见方式总结

    Redis是一个著名的key-value存储系统,也是nosql中的最常见的一种,这篇文章主要给大家总结了关于在java中jedis操作redis的几种常见方式,文中给出了详细的示例代码供大家参考学习 ...

  8. java代码操作Redis

    1.导入需要的pom依赖 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEn ...

  9. Java代码操作Redis的sentinel和Redis的集群Cluster操作

    总共四台机器,crxy99,crxy98分别是主节点和从节点.   crxy97和crxy96是两个监控此主从架构的sentinel节点. 看代码: import org.junit.Test; im ...

随机推荐

  1. bnu 51636 Squared Permutation 线段树

    Squared Permutation Time Limit: 6000ms Memory Limit: 262144KB 64-bit integer IO format: %lld      Ja ...

  2. async 和 await的前世今生 (转载)

    async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们编程埋下了一些隐 ...

  3. 代码中特殊的注释技术 -- TODO、FIXME和XXX的用处

    借鉴地址:https://www.cnblogs.com/pengyingh/articles/2445826.html 在阅读一些代码时,经常会遇到诸如:TODO.FIXME和XXX的单词,它们是有 ...

  4. TinyXML用法小结2

    参考:http://www.cnblogs.com/hgwang/p/5833638.html TinyXML用法小结 1.      介绍 Tinyxml的官方网址:http://www.grinn ...

  5. Java JDK5新特性-泛型

    2017-10-30 22:47:11 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质 ...

  6. SqlServer使用表值函数汇总

    先谈谈需求,我们先创建一张表,脚本如下: create table Cost ( Id ,) primary key,--编号 CostTime date,--时间 Num int--销售额 ); ' ...

  7. 20170724wdVBA正则表达式提取答案到Excel

    Public Sub RegExtractData() Dim StartTime, UsedTime StartTime = VBA.Timer Dim FilePath$ Dim FileName ...

  8. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.autumn.book/com.autumn.book.MainActivity}: android.os.NetworkOnMainThreadException

    不能把http请求写在主线程里,改为这样 Runnable runnable = new Runnable() { public void run() { HttpClient.post2(" ...

  9. 『cs231n』作业3问题2选讲_通过代码理解LSTM网络

    LSTM神经元行为分析 LSTM 公式可以描述如下: itftotgtctht=sigmoid(Wixxt+Wihht−1+bi)=sigmoid(Wfxxt+Wfhht−1+bf)=sigmoid( ...

  10. nyoj-833-博弈

    833-取石子(七) 内存限制:64MB 时间限制:1000ms 特判: No通过数:16 提交数:30 难度:1 题目描述: Yougth和Hrdv玩一个游戏,拿出n个石子摆成一圈,Yougth和H ...