源代码下载:   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. Jmeter 4.0 对返回Json处理

    研究这个的目的是:如果返回信息是比较复杂的,需要对返回信息中数据以及数据的层级做断言 我找了一些我们的接口,层级关系都太简单了,最复杂的就是这两个了:[而且都是get接口,原来get接口还要增加测试点 ...

  2. 日志_测试代码_Qt532

    1. int LogFile(QString &_str) { QDateTime datetime = QDateTime::currentDateTime();//获取系统现在的时间 QS ...

  3. Oracle 千万级别数据查询优化

    说明:平时很少接触到大数据分页,今天有兴趣在数据库插入1000万条数据进行测试,经过查询相关资料得到如下说明:笔者在工作中有一上百万条记录的表,在jsp页面中需对该表进行分页显示,便考虑用rownum ...

  4. 1.python+selenium利用cookie,跳过验证码直接登录

    方法1 在登录时,叫代码等待一段时间,然后手动输入验证码 # coding:utf-8 from selenium import webdriver import time url = 'http:/ ...

  5. yii CFormModel中的rules验证机制

    public function rules() { return array( array('username, password', 'required'), array('rememberMe', ...

  6. 雷林鹏分享:C# 类(Class)

    C# 类(Class) 当您定义一个类时,您定义了一个数据类型的蓝图.这实际上并没有定义任何的数据,但它定义了类的名称意味着什么,也就是说,类的对象由什么组成及在这个对象上可执行什么操作.对象是类的实 ...

  7. 雷林鹏分享:Ruby 变量

    Ruby 变量 变量是持有可被任何程序使用的任何数据的存储位置. Ruby 支持五种类型的变量.您已经在前面的章节中大概了解了这些变量,本章节将为您详细讲解这五种类型的变量. Ruby 全局变量 全局 ...

  8. 什么是SQL游标?

    1.1游标的概念 游标(Cursor)它使用户可逐行访问由SQL Server返回的结果集.使用游标(cursor)的一个主要的原因就是把集合操作转换成单个记录处理方式.用SQL语言从数据库中检索数据 ...

  9. WPF StoryBoard用法

    时间:2011-06-15 21:26来源:百度空间 作者:shichen4 点击: 次 StoryBoard使用,Xaml转cs代码 Canvas.Triggers EventTriggerRout ...

  10. php-fpm.conf配置文件中文说明详解及重要参数说明

    摘自:https://www.jb51.net/article/148550.htm 感谢分享 php-fpm工作流程 php-fpm全名是PHP FastCGI进程管理器 php-fpm启动后会先读 ...