Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存
源代码下载: 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; // 池化管理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 客户端
- * </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缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存的更多相关文章
- 【Redis】使用Jedis操作Redis
Jedis介绍 jedis就是集成了redis的一些命令操作,封装了redis的java客户端. Jedis使用 使用jedis需要引入jedis的jar包,下面提供了maven依赖 jedis.ja ...
- <Redis> 入门四 Jedis操作Redis
pom依赖 <dependencies> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> < ...
- Jedis操作Redis
Jedis操作Redis的常用封装方法 @Resource(name="jedispool") private JedisPool pool=null; /** * 设置缓存对象过 ...
- jedis操作redis的几种常见方式总结
Redis是一个著名的key-value存储系统,也是nosql中的最常见的一种,这篇文章主要给大家总结了关于在java中jedis操作redis的几种常见方式,文中给出了详细的示例代码供大家参考学习 ...
- Java中Jedis操作Redis与Spring的整合
Redis是一个key-value存储系统.它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类型都支持push/pop. ...
- Redis入门和Java利用jedis操作redis
Redis入门和Java利用jedis操作redis Redis介绍 Redis 是完全开源的,遵守 BSD 协议,是一个高性能的 key-value 数据库. Redis 与其他 key - val ...
- Jedis操作Redis数据库
添加Maven依赖: <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</grou ...
- 四、Jedis操作Redis
前言: 原来我们操作mysql需要用的jdbc,现在操作redis则需要jedis,jedis是客户端,而redis是服务器,使用jedis客户端来操作redis. 在这里要使用jedis操作red ...
- JAVA中通过Jedis操作Redis连接与插入简单库
一.简述 JAVA中通过Jedis操作Redis连接与插入简单库 二.依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis ...
- 第三百节,python操作redis缓存-其他常用操作,用于操作redis里的数据name,不论什么数据类型
python操作redis缓存-其他常用操作,用于操作redis里的数据name,不论什么数据类型 delete(*names)根据删除redis中的任意数据类型 #!/usr/bin/env pyt ...
随机推荐
- poj 1611 The Suspects(简单并查集)
题目:http://poj.org/problem?id=1611 0号是病原,求多少人有可能感染 #include<stdio.h> #include<string.h> # ...
- 在Visual Studio 2010中使用DSL Tool特定领域开发 开篇
本来是很想写关于VS的DSL的文章的,有点小忙,就一直在拖延,忽然有看见了"<在Visual Studio 2012中使用VMSDK开发特定领域语言>",又有写的欲望了 ...
- HDU 3549 Flow Problem 流问题(最大流,入门)
题意:给个赤裸的最大流问题. 思路:EK+BFS解决.跟HDU1532几乎一样的. #include <bits/stdc++.h> #define LL long long #defin ...
- Bootstrap 源码解析(转)
1.Bootstrap的作用域 2.Bootstrap的类定义 3.Bootstrap的插件定义 4.Bootstrap的事件代理 5.Bootstrap的对象数据缓存 6.Bootstrap的防冲突 ...
- 【聚类算法】谱聚类(Spectral Clustering)
目录: 1.问题描述 2.问题转化 3.划分准则 4.总结 1.问题描述 谱聚类(Spectral Clustering, SC)是一种基于图论的聚类方法——将带权无向图划分为两个或两个以上的最优子图 ...
- 嵌入式Linux启动过程中的问题积累
嵌入式Linux启动过程中的问题积累 Dongas 07-12-19 1.Bad Magic Number ## Booting image at 33000000 ... Bad Magic Num ...
- (3)java棧
java棧和函数调用的关系图 [名词解释]--->java棧是一块线程的私有空间--->java的棧是先进后出的数据结构.函数返回,则该函数的棧帧被弹出.--->一个函数对应一个棧帧 ...
- ClassLoader工作机制
阅读目录 一.ClassLoader概念 二.JVM平台提供三层classLoader 三.JVM加载class文件到内存有两种方式 四.ClassLoader加载类的过程 五.自定义类加载器 六.实 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.3.1
Let $A=A_1\oplus A_2$. Show that (1). $W(A)$ is the convex hull of $W(A_1)$ and $W(A_2)$; i.e., the ...
- java jvm学习笔记三(class文件检验器)
欢迎装载请说明出处:http://blog.csdn.net/yfqnihao 前面的学习我们知道了class文件被类装载器所装载,但是在装载class文件之前或之后,class文件实际上还需要被校验 ...