日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

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

發(fā)布時(shí)間:2025/4/16 数据库 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

源代碼下載:?http://download.csdn.net/detail/jiangtao_st/7623113

?

1、Maven配置

?

[html]?view plaincopyprint?
  • <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、代碼具體實(shí)現(xiàn)的Client

    ?

    [java]?view plaincopyprint?
  • /**?
  • ?*??
  • ?*?<p>?
  • ?*??Redis客戶端訪問?
  • ?*?</p>?
  • ?*??
  • ?*?@author?卓軒?
  • ?*?@創(chuàng)建時(shí)間:2014年7月11日?
  • ?*?@version:?V1.0?
  • ?*/??
  • public?class?RedisClient?{??
  • ??????
  • ????public??static??JedisPool?jedisPool;?//?池化管理jedis鏈接池??
  • ??????
  • ????static?{??
  • ??????????
  • ????????//讀取相關(guān)的配置??
  • ????????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();????
  • ????????//設(shè)置最大連接數(shù)??
  • ????????config.setMaxTotal(maxActive);??
  • ????????//設(shè)置最大空閑數(shù)??
  • ????????config.setMaxIdle(maxIdle);??
  • ????????//設(shè)置超時(shí)時(shí)間??
  • ????????config.setMaxWaitMillis(maxWait);??
  • ??????????
  • ????????//初始化連接池??
  • ????????jedisPool?=?new?JedisPool(config,?ip,?port);???
  • ????}??
  • ??????
  • ????/**?
  • ?????*?向緩存中設(shè)置字符串內(nèi)容?
  • ?????*?@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);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?向緩存中設(shè)置對(duì)象?
  • ?????*?@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);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?刪除緩存中得對(duì)象,根據(jù)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);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?根據(jù)key?獲取內(nèi)容?
  • ?????*?@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);??
  • ????????}??
  • ????}??
  • ??????
  • ??
  • ????/**?
  • ?????*?根據(jù)key?獲取對(duì)象?
  • ?????*?@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 分片管理

    ?

    [java]?view plaincopyprint?
  • /**?
  • ?*??
  • ?*?<p>?
  • ?*?Sharding?Redis?Client?工具類?
  • ?*?</p>?
  • ?*??
  • ?*?@author?卓軒?
  • ?*?@創(chuàng)建時(shí)間:2014年7月11日?
  • ?*?@version:?V1.0?
  • ?*/??
  • public?class?ShardingRedisClient?{??
  • ??
  • ????private?static?ShardedJedisPool?shardedJedisPool;??
  • ??
  • ????static?{??
  • ????????//?讀取相關(guān)的配置??
  • ????????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"));??
  • ??????????
  • ????????//設(shè)置配置??
  • ????????JedisPoolConfig?config?=?new?JedisPoolConfig();??
  • ????????config.setMaxTotal(maxActive);??
  • ????????config.setMaxIdle(maxIdle);??
  • ????????config.setMaxWaitMillis(maxWait);??
  • ??????????
  • ????????//設(shè)置分片元素信息??
  • ????????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);??
  • ????}??
  • ??
  • ??????
  • ????/**?
  • ?????*?向緩存中設(shè)置字符串內(nèi)容?
  • ?????*?@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);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?向緩存中設(shè)置對(duì)象?
  • ?????*?@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);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?刪除緩存中得對(duì)象,根據(jù)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);??
  • ????????}??
  • ????}??
  • ??????
  • ????/**?
  • ?????*?根據(jù)key?獲取內(nèi)容?
  • ?????*?@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);??
  • ????????}??
  • ????}??
  • ??????
  • ??
  • ????/**?
  • ?????*?根據(jù)key?獲取對(duì)象?
  • ?????*?@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、 單元測(cè)試、保存對(duì)象、寫入對(duì)象

    ?

    ?

    [java]?view plaincopyprint?
  • /**?
  • ?*??
  • ?*?<p>?
  • ?*??測(cè)試獨(dú)立redis?客戶端?
  • ?*?</p>?
  • ?*??
  • ?*?@author?卓軒?
  • ?*?@創(chuàng)建時(shí)間:2014年7月11日?
  • ?*?@version:?V1.0?
  • ?*/??
  • public?class?SimpleClient?{??
  • ??????
  • ????@Test??
  • ????public?void?userCache(){??
  • ??????????
  • ????????//向緩存中保存對(duì)象??
  • ????????UserDO?zhuoxuan?=?new?UserDO();??
  • ????????zhuoxuan.setUserId(113445);??
  • ????????zhuoxuan.setSex(1);??
  • ????????zhuoxuan.setUname("卓軒");??
  • ????????zhuoxuan.setUnick("zhuoxuan");??
  • ????????zhuoxuan.setEmail("zhuoxuan@mogujie.com");??
  • ????????//調(diào)用方法處理??
  • ????????boolean?reusltCache?=?RedisClient.set("zhuoxuan",?zhuoxuan);??
  • ????????if?(reusltCache)?{??
  • ????????????System.out.println("向緩存中保存對(duì)象成功。");??
  • ????????}else{??
  • ????????????System.out.println("向緩存中保存對(duì)象失敗。");??
  • ????????}??
  • ????}??
  • ??????
  • ??????
  • ????@Test??
  • ????public?void?getUserInfo(){??
  • ??????????
  • ????????UserDO?zhuoxuan?=?RedisClient.get("zhuoxuan",UserDO.class);??
  • ????????if(zhuoxuan?!=?null){??
  • ????????????System.out.println("從緩存中獲取的對(duì)象,"?+?zhuoxuan.getUname()?+?"@"?+?zhuoxuan.getEmail());??
  • ????????}??
  • ??????????
  • ????}??
  • ??????
  • ??????
  • ??
  • } ?
  • 轉(zhuǎn)載于:https://www.cnblogs.com/fx2008/p/4155146.html

    總結(jié)

    以上是生活随笔為你收集整理的Redis缓存 ava-Jedis操作Redis,基本操作以及 实现对象保存的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。