zoukankan      html  css  js  c++  java
  • java redis 工具类

     1 package com.mohecun.jedis;
     2 
     3 public interface JedisClient {
     4 
     5     String set(String key, String value);
     6     String get(String key);
     7     Boolean exists(String key);
     8     Long expire(String key, int seconds);
     9     Long ttl(String key);
    10     Long incr(String key);
    11     Long hset(String key, String field, String value);
    12     String hget(String key, String field);
    13     Long hdel(String key, String... field);
    14 }

    clusterClient:

     1 package com.mohecun.jedis;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 
     5 import redis.clients.jedis.JedisCluster;
     6 
     7 public class JedisClientCluster implements JedisClient {
     8     
     9     @Autowired
    10     private JedisCluster jedisCluster;
    11 
    12     @Override
    13     public String set(String key, String value) {
    14         return jedisCluster.set(key, value);
    15     }
    16 
    17     @Override
    18     public String get(String key) {
    19         return jedisCluster.get(key);
    20     }
    21 
    22     @Override
    23     public Boolean exists(String key) {
    24         return jedisCluster.exists(key);
    25     }
    26 
    27     @Override
    28     public Long expire(String key, int seconds) {
    29         return jedisCluster.expire(key, seconds);
    30     }
    31 
    32     @Override
    33     public Long ttl(String key) {
    34         return jedisCluster.ttl(key);
    35     }
    36 
    37     @Override
    38     public Long incr(String key) {
    39         return jedisCluster.incr(key);
    40     }
    41 
    42     @Override
    43     public Long hset(String key, String field, String value) {
    44         return jedisCluster.hset(key, field, value);
    45     }
    46 
    47     @Override
    48     public String hget(String key, String field) {
    49         return jedisCluster.hget(key, field);
    50     }
    51 
    52     @Override
    53     public Long hdel(String key, String... field) {
    54         return jedisCluster.hdel(key, field);
    55     }
    56 
    57 }

    clientPool:

     1 package com.mohecun.jedis;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 
     5 import redis.clients.jedis.Jedis;
     6 import redis.clients.jedis.JedisPool;
     7 
     8 public class JedisClientPool implements JedisClient {
     9     
    10     @Autowired
    11     private JedisPool jedisPool;
    12 
    13     @Override
    14     public String set(String key, String value) {
    15         Jedis jedis = jedisPool.getResource();
    16         String result = jedis.set(key, value);
    17         jedis.close();
    18         return result;
    19     }
    20 
    21     @Override
    22     public String get(String key) {
    23         Jedis jedis = jedisPool.getResource();
    24         String result = jedis.get(key);
    25         jedis.close();
    26         return result;
    27     }
    28 
    29     @Override
    30     public Boolean exists(String key) {
    31         Jedis jedis = jedisPool.getResource();
    32         Boolean result = jedis.exists(key);
    33         jedis.close();
    34         return result;
    35     }
    36 
    37     @Override
    38     public Long expire(String key, int seconds) {
    39         Jedis jedis = jedisPool.getResource();
    40         Long result = jedis.expire(key, seconds);
    41         jedis.close();
    42         return result;
    43     }
    44 
    45     @Override
    46     public Long ttl(String key) {
    47         Jedis jedis = jedisPool.getResource();
    48         Long result = jedis.ttl(key);
    49         jedis.close();
    50         return result;
    51     }
    52 
    53     @Override
    54     public Long incr(String key) {
    55         Jedis jedis = jedisPool.getResource();
    56         Long result = jedis.incr(key);
    57         jedis.close();
    58         return result;
    59     }
    60 
    61     @Override
    62     public Long hset(String key, String field, String value) {
    63         Jedis jedis = jedisPool.getResource();
    64         Long result = jedis.hset(key, field, value);
    65         jedis.close();
    66         return result;
    67     }
    68 
    69     @Override
    70     public String hget(String key, String field) {
    71         Jedis jedis = jedisPool.getResource();
    72         String result = jedis.hget(key, field);
    73         jedis.close();
    74         return result;
    75     }
    76 
    77     @Override
    78     public Long hdel(String key, String... field) {
    79         Jedis jedis = jedisPool.getResource();
    80         Long result = jedis.hdel(key, field);
    81         jedis.close();
    82         return result;
    83     }
    84 
    85 }
  • 相关阅读:
    day3 数据类型
    子查询
    mysql综合练习题
    day5 练习
    月末总结
    Iconfont-阿里巴巴矢量图标库
    vue简介
    Redis简介和数据结构
    浏览器初始化css
    vue脚手架搭建项目初始化
  • 原文地址:https://www.cnblogs.com/volatileAndCrazy/p/7843654.html
Copyright © 2011-2022 走看看