Java操作redis之一(对象保存、Pool设置、sharding)

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

    1、Maven配置

     

    01. <dependency>
    02. <groupId>redis.clients</groupId>
    03. <artifactId>jedis</artifactId>
    04. <version>2.5.0</version>
    05. </dependency>
    06. <dependency>
    07. <groupId>com.alibaba</groupId>
    08. <artifactId>fastjson</artifactId>
    09. <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

     

    001. /**
    002. *
    003. * <p>
    004. *  Redis客户端访问
    005. * </p>
    006. *
    007. * @author 卓轩
    008. * @创建时间:2014年7月11日
    009. * @version: V1.0
    010. */
    011. public class RedisClient {
    012.  
    013. public  static  JedisPool jedisPool; // 池化管理jedis链接池
    014.  
    015. static {
    016.  
    017. //读取相关的配置
    018. ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
    019. int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
    020. int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
    021. int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
    022.  
    023. String ip = resourceBundle.getString("redis.ip");
    024. int port = Integer.parseInt(resourceBundle.getString("redis.port"));
    025.  
    026. JedisPoolConfig config = new JedisPoolConfig(); 
    027. //设置最大连接数
    028. config.setMaxTotal(maxActive);
    029. //设置最大空闲数
    030. config.setMaxIdle(maxIdle);
    031. //设置超时时间
    032. config.setMaxWaitMillis(maxWait);
    033.  
    034. //初始化连接池
    035. jedisPool = new JedisPool(config, ip, port);
    036. }
    037.  
    038. /**
    039. * 向缓存中设置字符串内容
    040. * @param key key
    041. * @param value value
    042. * @return
    043. * @throws Exception
    044. */
    045. public static boolean  set(String key,String value) throws Exception{
    046. Jedis jedis = null;
    047. try {
    048. jedis = jedisPool.getResource();
    049. jedis.set(key, value);
    050. return true;
    051. catch (Exception e) {
    052. e.printStackTrace();
    053. return false;
    054. }finally{
    055. jedisPool.returnResource(jedis);
    056. }
    057. }
    058.  
    059. /**
    060. * 向缓存中设置对象
    061. * @param key
    062. * @param value
    063. * @return
    064. */
    065. public static boolean  set(String key,Object value){
    066. Jedis jedis = null;
    067. try {
    068. String objectJson = JSON.toJSONString(value);
    069. jedis = jedisPool.getResource();
    070. jedis.set(key, objectJson);
    071. return true;
    072. catch (Exception e) {
    073. e.printStackTrace();
    074. return false;
    075. }finally{
    076. jedisPool.returnResource(jedis);
    077. }
    078. }
    079.  
    080. /**
    081. * 删除缓存中得对象,根据key
    082. * @param key
    083. * @return
    084. */
    085. public static boolean del(String key){
    086. Jedis jedis = null;
    087. try {
    088. jedis = jedisPool.getResource();
    089. jedis.del(key);
    090. return true;
    091. catch (Exception e) {
    092. e.printStackTrace();
    093. return false;
    094. }finally{
    095. jedisPool.returnResource(jedis);
    096. }
    097. }
    098.  
    099. /**
    100. * 根据key 获取内容
    101. * @param key
    102. * @return
    103. */
    104. public static Object get(String key){
    105. Jedis jedis = null;
    106. try {
    107. jedis = jedisPool.getResource();
    108. Object value = jedis.get(key);
    109. return value;
    110. catch (Exception e) {
    111. e.printStackTrace();
    112. return false;
    113. }finally{
    114. jedisPool.returnResource(jedis);
    115. }
    116. }
    117.  
    118.  
    119. /**
    120. * 根据key 获取对象
    121. * @param key
    122. * @return
    123. */
    124. public static <T> T get(String key,Class<T> clazz){
    125. Jedis jedis = null;
    126. try {
    127. jedis = jedisPool.getResource();
    128. String value = jedis.get(key);
    129. return JSON.parseObject(value, clazz);
    130. catch (Exception e) {
    131. e.printStackTrace();
    132. return null;
    133. }finally{
    134. jedisPool.returnResource(jedis);
    135. }
    136. }
    137.  
    138.  
    139. }

    4、Sharding 分片管理

     

    001. /**
    002. *
    003. * <p>
    004. * Sharding Redis Client 工具类
    005. * </p>
    006. *
    007. * @author 卓轩
    008. * @创建时间:2014年7月11日
    009. * @version: V1.0
    010. */
    011. public class ShardingRedisClient {
    012.  
    013. private static ShardedJedisPool shardedJedisPool;
    014.  
    015. static {
    016. // 读取相关的配置
    017. ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");
    018. int maxActive = Integer.parseInt(resourceBundle.getString("redis.pool.maxActive"));
    019. int maxIdle = Integer.parseInt(resourceBundle.getString("redis.pool.maxIdle"));
    020. int maxWait = Integer.parseInt(resourceBundle.getString("redis.pool.maxWait"));
    021.  
    022. String ip = resourceBundle.getString("redis.ip");
    023. int port = Integer.parseInt(resourceBundle.getString("redis.port"));
    024.  
    025. //设置配置
    026. JedisPoolConfig config = new JedisPoolConfig();
    027. config.setMaxTotal(maxActive);
    028. config.setMaxIdle(maxIdle);
    029. config.setMaxWaitMillis(maxWait);
    030.  
    031. //设置分片元素信息
    032. JedisShardInfo shardInfo1 = new JedisShardInfo(ip,port);
    033. JedisShardInfo shardInfo2 = new JedisShardInfo(ip,port);
    034. List<JedisShardInfo> list = new ArrayList<JedisShardInfo>();
    035. list.add(shardInfo1);
    036. list.add(shardInfo2);
    037. shardedJedisPool = new ShardedJedisPool(config, list);
    038. }
    039.  
    040.  
    041. /**
    042. * 向缓存中设置字符串内容
    043. * @param key key
    044. * @param value value
    045. * @return
    046. * @throws Exception
    047. */
    048. public static boolean  set(String key,String value) throws Exception{
    049. ShardedJedis jedis = null;
    050. try {
    051. jedis = shardedJedisPool.getResource();
    052. jedis.set(key, value);
    053. return true;
    054. catch (Exception e) {
    055. e.printStackTrace();
    056. return false;
    057. }finally{
    058. shardedJedisPool.returnResource(jedis);
    059. }
    060. }
    061.  
    062. /**
    063. * 向缓存中设置对象
    064. * @param key
    065. * @param value
    066. * @return
    067. */
    068. public static boolean  set(String key,Object value){
    069. ShardedJedis jedis = null;
    070. try {
    071. String objectJson = JSON.toJSONString(value);
    072. jedis = shardedJedisPool.getResource();
    073. jedis.set(key, objectJson);
    074. return true;
    075. catch (Exception e) {
    076. e.printStackTrace();
    077. return false;
    078. }finally{
    079. shardedJedisPool.returnResource(jedis);
    080. }
    081. }
    082.  
    083. /**
    084. * 删除缓存中得对象,根据key
    085. * @param key
    086. * @return
    087. */
    088. public static boolean del(String key){
    089. ShardedJedis jedis = null;
    090. try {
    091. jedis = shardedJedisPool.getResource();
    092. jedis.del(key);
    093. return true;
    094. catch (Exception e) {
    095. e.printStackTrace();
    096. return false;
    097. }finally{
    098. shardedJedisPool.returnResource(jedis);
    099. }
    100. }
    101.  
    102. /**
    103. * 根据key 获取内容
    104. * @param key
    105. * @return
    106. */
    107. public static Object get(String key){
    108. ShardedJedis jedis = null;
    109. try {
    110. jedis = shardedJedisPool.getResource();
    111. Object value = jedis.get(key);
    112. return value;
    113. catch (Exception e) {
    114. e.printStackTrace();
    115. return false;
    116. }finally{
    117. shardedJedisPool.returnResource(jedis);
    118. }
    119. }
    120.  
    121.  
    122. /**
    123. * 根据key 获取对象
    124. * @param key
    125. * @return
    126. */
    127. public static <T> T get(String key,Class<T> clazz){
    128. ShardedJedis jedis = null;
    129. try {
    130. jedis = shardedJedisPool.getResource();
    131. String value = jedis.get(key);
    132. return JSON.parseObject(value, clazz);
    133. catch (Exception e) {
    134. e.printStackTrace();
    135. return null;
    136. }finally{
    137. shardedJedisPool.returnResource(jedis);
    138. }
    139. }
    140.  
    141. }


     

     

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

     

     

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

你可能感兴趣的:(redis)