Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存

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

1、Maven配置

[html] view plaincopyprint?

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. <version>2.5.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba</groupId>
  8. <artifactId>fastjson</artifactId>
  9. <version>1.1.41</version>
  10. </dependency></span>

2、Properties 配置文件

redis.pool.maxActive=100

redis.pool.maxIdle=20

redis.pool.maxWait=3000

redis.ip=localhost

redis.port=6379

3、代码具体实现的Client

[java] view plaincopyprint?

  1. /**
  2. *
  3. * <p>
  4. *  Redis客户端访问
  5. * </p>
  6. *
  7. * @author 卓轩
  8. * @创建时间:2014年7月11日
  9. * @version: V1.0
  10. */
  11. public class RedisClient {
  12. public  static  JedisPool jedisPool; // 池化管理jedis链接池
  13. static {
  14. //读取相关的配置
  15. ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
  16. int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
  17. int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
  18. int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
  19. String ip = resourceBundle.getString("redis.ip");
  20. int port = Integer.parseInt(resourceBundle.getString("redis.port"));
  21. JedisPoolConfig config = new JedisPoolConfig();
  22. //设置最大连接数
  23. config.setMaxTotal(maxActive);
  24. //设置最大空闲数
  25. config.setMaxIdle(maxIdle);
  26. //设置超时时间
  27. config.setMaxWaitMillis(maxWait);
  28. //初始化连接池
  29. jedisPool = new JedisPool(config, ip, port);
  30. }
  31. /**
  32. * 向缓存中设置字符串内容
  33. * @param key key
  34. * @param value value
  35. * @return
  36. * @throws Exception
  37. */
  38. public static boolean  set(String key,String value) throws Exception{
  39. Jedis jedis = null;
  40. try {
  41. jedis = jedisPool.getResource();
  42. jedis.set(key, value);
  43. return true;
  44. } catch (Exception e) {
  45. e.printStackTrace();
  46. return false;
  47. }finally{
  48. jedisPool.returnResource(jedis);
  49. }
  50. }
  51. /**
  52. * 向缓存中设置对象
  53. * @param key
  54. * @param value
  55. * @return
  56. */
  57. public static boolean  set(String key,Object value){
  58. Jedis jedis = null;
  59. try {
  60. String objectJson = JSON.toJSONString(value);
  61. jedis = jedisPool.getResource();
  62. jedis.set(key, objectJson);
  63. return true;
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. return false;
  67. }finally{
  68. jedisPool.returnResource(jedis);
  69. }
  70. }
  71. /**
  72. * 删除缓存中得对象,根据key
  73. * @param key
  74. * @return
  75. */
  76. public static boolean del(String key){
  77. Jedis jedis = null;
  78. try {
  79. jedis = jedisPool.getResource();
  80. jedis.del(key);
  81. return true;
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. return false;
  85. }finally{
  86. jedisPool.returnResource(jedis);
  87. }
  88. }
  89. /**
  90. * 根据key 获取内容
  91. * @param key
  92. * @return
  93. */
  94. public static Object get(String key){
  95. Jedis jedis = null;
  96. try {
  97. jedis = jedisPool.getResource();
  98. Object value = jedis.get(key);
  99. return value;
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. return false;
  103. }finally{
  104. jedisPool.returnResource(jedis);
  105. }
  106. }
  107. /**
  108. * 根据key 获取对象
  109. * @param key
  110. * @return
  111. */
  112. public static <T> T get(String key,Class<T> clazz){
  113. Jedis jedis = null;
  114. try {
  115. jedis = jedisPool.getResource();
  116. String value = jedis.get(key);
  117. return JSON.parseObject(value, clazz);
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. return null;
  121. }finally{
  122. jedisPool.returnResource(jedis);
  123. }
  124. }
  125. }

4、Sharding 分片管理

[java] view plaincopyprint?

  1. /**
  2. *
  3. * <p>
  4. * Sharding Redis Client 工具类
  5. * </p>
  6. *
  7. * @author 卓轩
  8. * @创建时间:2014年7月11日
  9. * @version: V1.0
  10. */
  11. public class ShardingRedisClient {
  12. private static ShardedJedisPool shardedJedisPool;
  13. static {
  14. // 读取相关的配置
  15. ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
  16. int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
  17. int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
  18. int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
  19. String ip = resourceBundle.getString("redis.ip");
  20. int port = Integer.parseInt(resourceBundle.getString("redis.port"));
  21. //设置配置
  22. JedisPoolConfig config = new JedisPoolConfig();
  23. config.setMaxTotal(maxActive);
  24. config.setMaxIdle(maxIdle);
  25. config.setMaxWaitMillis(maxWait);
  26. //设置分片元素信息
  27. JedisShardInfo shardInfo1 = new JedisShardInfo(ip,port);
  28. JedisShardInfo shardInfo2 = new JedisShardInfo(ip,port);
  29. List<JedisShardInfo> list = new ArrayList<JedisShardInfo>();
  30. list.add(shardInfo1);
  31. list.add(shardInfo2);
  32. shardedJedisPool = new ShardedJedisPool(config, list);
  33. }
  34. /**
  35. * 向缓存中设置字符串内容
  36. * @param key key
  37. * @param value value
  38. * @return
  39. * @throws Exception
  40. */
  41. public static boolean  set(String key,String value) throws Exception{
  42. ShardedJedis jedis = null;
  43. try {
  44. jedis = shardedJedisPool.getResource();
  45. jedis.set(key, value);
  46. return true;
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. return false;
  50. }finally{
  51. shardedJedisPool.returnResource(jedis);
  52. }
  53. }
  54. /**
  55. * 向缓存中设置对象
  56. * @param key
  57. * @param value
  58. * @return
  59. */
  60. public static boolean  set(String key,Object value){
  61. ShardedJedis jedis = null;
  62. try {
  63. String objectJson = JSON.toJSONString(value);
  64. jedis = shardedJedisPool.getResource();
  65. jedis.set(key, objectJson);
  66. return true;
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. return false;
  70. }finally{
  71. shardedJedisPool.returnResource(jedis);
  72. }
  73. }
  74. /**
  75. * 删除缓存中得对象,根据key
  76. * @param key
  77. * @return
  78. */
  79. public static boolean del(String key){
  80. ShardedJedis jedis = null;
  81. try {
  82. jedis = shardedJedisPool.getResource();
  83. jedis.del(key);
  84. return true;
  85. } catch (Exception e) {
  86. e.printStackTrace();
  87. return false;
  88. }finally{
  89. shardedJedisPool.returnResource(jedis);
  90. }
  91. }
  92. /**
  93. * 根据key 获取内容
  94. * @param key
  95. * @return
  96. */
  97. public static Object get(String key){
  98. ShardedJedis jedis = null;
  99. try {
  100. jedis = shardedJedisPool.getResource();
  101. Object value = jedis.get(key);
  102. return value;
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. return false;
  106. }finally{
  107. shardedJedisPool.returnResource(jedis);
  108. }
  109. }
  110. /**
  111. * 根据key 获取对象
  112. * @param key
  113. * @return
  114. */
  115. public static <T> T get(String key,Class<T> clazz){
  116. ShardedJedis jedis = null;
  117. try {
  118. jedis = shardedJedisPool.getResource();
  119. String value = jedis.get(key);
  120. return JSON.parseObject(value, clazz);
  121. } catch (Exception e) {
  122. e.printStackTrace();
  123. return null;
  124. }finally{
  125. shardedJedisPool.returnResource(jedis);
  126. }
  127. }
  128. }

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

[java] view plaincopyprint?

    1. /**
    2. *
    3. * <p>
    4. *  测试独立redis 客户端
    5. * </p>
    6. *
    7. * @author 卓轩
    8. * @创建时间:2014年7月11日
    9. * @version: V1.0
    10. */
    11. public class SimpleClient {
    12. @Test
    13. public void userCache(){
    14. //向缓存中保存对象
    15. UserDO zhuoxuan = new UserDO();
    16. zhuoxuan.setUserId(113445);
    17. zhuoxuan.setSex(1);
    18. zhuoxuan.setUname("卓轩");
    19. zhuoxuan.setUnick("zhuoxuan");
    20. zhuoxuan.setEmail("[email protected]");
    21. //调用方法处理
    22. boolean reusltCache = RedisClient.set("zhuoxuan", zhuoxuan);
    23. if (reusltCache) {
    24. System.out.println("向缓存中保存对象成功。");
    25. }else{
    26. System.out.println("向缓存中保存对象失败。");
    27. }
    28. }
    29. @Test
    30. public void getUserInfo(){
    31. UserDO zhuoxuan = RedisClient.get("zhuoxuan",UserDO.class);
    32. if(zhuoxuan != null){
    33. System.out.println("从缓存中获取的对象," + zhuoxuan.getUname() + "@" + zhuoxuan.getEmail());
    34. }
    35. }
    36. }
时间: 2024-08-02 10:05:37

Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存的相关文章

Redis学习-5 Jedis操作Redis

1.Jedis Jedis在java应用中操作Redis; 2.Jedis访问Redis 下载Jedis http://search.maven.org和Commons-Poolx [关闭linux的防火墙] 新建java project 导入jar jedis的jar 新建class,创建jedis对象,连接到Redis服务器, String host = "192.168.1.1"; int port = 6379; Jedis jedis = new Jedis(host,por

【Redis】使用Jedis操作Redis

https://www.cnblogs.com/relucent/p/4203190.html Jedis介绍 jedis就是集成了redis的一些命令操作,封装了redis的java客户端. Jedis使用 使用jedis需要引入jedis的jar包,下面提供了maven依赖 jedis.jar是封装的包,commons-pool2.jar是管理连接的包 1 <!-- https://mvnrepository.com/artifact/redis.clients/jedis 客户端-->

Redis缓存系统(一)Java-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> <

Java中Jedis操作Redis与Spring的整合

Redis是一个key-value存储系统.它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有序集合).这些数据类型都支持push/pop.add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的.在此基础上,redis支持各种不同方式的排序.为了保证效率,数据都是缓存在内存中.redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步.以下是

redis缓存工具Jedis进行跨jvm加锁(分布式应用)

最近使用redis碰到了多个并发处理同一个缓存的情况.在这种情况下需要进行加锁机制.本来想使用java自带的ReadWriteLock进行设置读写锁,这也是上家公司使用的方法.后来经过商讨,给予排除.原因无他,就是java自带的并不能跨jvm进行加锁,意思就是说A服务器上的write锁无法限制B服务器上的同一个方法,也就是说不适用于分布式部署的环境. 后来经过多方面查看资料.最终决定使用redis自身的方法setnx来进行加锁机制.网上有很多关于setnx来进行加锁的方法.不过大部分都会有一个相

四、Jedis操作Redis

前言:  原来我们操作mysql需要用的jdbc,现在操作redis则需要jedis,jedis是客户端,而redis是服务器,使用jedis客户端来操作redis. 在这里要使用jedis操作redis需要引入下面两个jar包 一.Jedis简单操作 1.使用jedis 客户端,完成jedis简单操作: public class JedisTest { @Test() public void setRedis(){ Jedis connection = new Jedis("127.0.0.1

Redis介绍 &amp;&amp; Java客户端操作Redis

Redis介绍 && Java客户端操作Redis 本文内容 redis介绍 redis的 shell 客户端简介 redis的 java 客户端简介 环境配置 redis 2.8.17 64bit JDK1.6 redis介绍 大多数时候,我们都将 redis 称作是内存数据库,它在运行过程中,将键值对信息存储在内存中,同时在后台以异步的形式写入本地数据库中(默认是:dump.rdb,在 redis.conf 中配置,如果需要考虑安全的持久化需求需要开启 AOF 功能,详细介绍可以查看这

使用Jedis操作redis 缓存

案例:http://blog.csdn.net/linlzk/article/details/41801391 Redis是一个开源的Key-Value数据缓存,和Memcached类似. Redis多种类型的value,包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈希类型). Jedis 是 Redis 官方首选的 Java 客户端开发包. 1 //连接redis ,redis的默认端口是6379 2 3 Jedis

使用Jedis操作redis

使用Java操作Redis需要jedis-2.1.0.jar,下载地址:http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip 如果需要使用Redis连接池的话,还需commons-pool-1.5.4.jar,下载地址:http://files.cnblogs.com/liuling/commons-pool-1.5.4.jar.zip jedis的基本操作: package com.donghai.redis; import java.uti

Spring-Boot 使用 Jedis 操作 Redis

背景: 1.Redis 之前学了个皮毛 还忘的差不多了,感觉公司项目中的Redis用的真的牛逼,so 需要深造. 2.有个同事在搞Jedis,勾起了我对知识的向往,不会用,但是很渴望. 过程: 1.改造原有项目集成Jedis,引入jar包 <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.8.2</ver