zoukankan      html  css  js  c++  java
  • springMVC+redis+redis自定义工具类 的配置

    1.

    maven项目,加入这一个依赖包即可,

    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.9.0</version>
    </dependency>
    使用2.9.0版本的原因是稳定,且3以上的版本有问题,部分参数会缺失


    2.

    
    

    配置文件redis.properties,

    
    
    redis.hostname=127.0.0.1
    redis.port=6379
    redis.database=0
    redis.pool.maxActive=600
    redis.pool.maxIdle=300
    redis.pool.maxWait=3000
    redis.pool.testOnBorrow=true
    redis.password=853396015
    redis.timeout=2000

    3.

    新建一个xml文件,名为 redisSpringContext.xml ,打开折叠可查看

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:context="http://www.springframework.org/schema/context"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     7         http://www.springframework.org/schema/context
     8         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
     9 
    10     <!--    &lt;!&ndash; 导入redis数据库配置文件 &ndash;&gt;-->
    11     <context:property-placeholder location="classpath:redis.properties"/>
    12 
    13     <!-- Redis连接池配置 -->
    14     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    15         <!-- 控制一个pool能分配多少个jedis实例 -->
    16         <property name="maxTotal" value="${redis.pool.maxActive}"/>
    17         <!-- 连接池中最多空闲多少个maxIdle个连接,这里为20,表示即使没有数据库连接时依然可以保持20空闲的连接,而不被清除,处于待命状态,随时连接 -->
    18         <property name="maxIdle" value="${redis.pool.maxIdle}"/>
    19         <!-- 最大等待时间,当没有可用连接时,连接池等待连接被归还的最大时间(以毫秒计数),超过时间即抛出异常 -->
    20         <property name="maxWaitMillis" value="${redis.pool.maxWait}"/>
    21         <!-- 在获取连接时,检查有效性 -->
    22         <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
    23     </bean>
    24     <!-- 创建Redis连接池,并做相关配置 -->
    25     <bean id="jedisWritePool" class="redis.JedisPoolWriper"
    26           depends-on="jedisPoolConfig">
    27         <constructor-arg index="0" ref="jedisPoolConfig"/>
    28         <constructor-arg index="1" value="${redis.hostname}"/>
    29         <constructor-arg index="2" value="${redis.port}" type="int"/>
    30         <constructor-arg index="3" value="${redis.timeout}" type="int"/>
    31         <constructor-arg index="4" value="${redis.password}"/>
    32     </bean>
    33     <!-- 创建Redis工具类,封装好Redis的连接以进行相关操作 -->
    34     <bean id="jedisUtil" class="redis.JedisUtil"
    35     >
    36         <property name="jedisPool" ref="jedisWritePool"/>
    37     </bean>
    38 <!--    这里的红色下划线提示不是指写错了,而是警告,如果使用自动补全修正,会报错UnsatisfiedDependencyException
    39 为什么使用$进行隔开呢,是因为这是两个类嵌套定义,因为不是文件路径,无法使用.符号进行区分,故使用$符号时没问题的
    40 -->
    41     <bean id="jedisKeys" class="redis.JedisUtil$Keys"
    42     >
    43         <constructor-arg ref="jedisUtil"/>
    44     </bean>
    45     <bean id="jedisStrings" class="redis.JedisUtil$Strings"
    46     >
    47         <constructor-arg ref="jedisUtil"/>
    48     </bean>
    49     <bean id="jedisLists" class="redis.JedisUtil$Lists"
    50     >
    51         <constructor-arg ref="jedisUtil"/>
    52     </bean>
    53     <bean id="jedisSets" class="redis.JedisUtil$Sets"
    54     >
    55         <constructor-arg ref="jedisUtil"/>
    56     </bean>
    57     <bean id="jedisHash" class="redis.JedisUtil$Hash"
    58           scope="singleton">
    59         <constructor-arg ref="jedisUtil"/>
    60     </bean>
    61 
    62 </beans>
    redisSpringContext.xml



    记得将该xml文件导入
    dispatcher-servlet.xml中

    <import resource="redisSpringContext.xml"/>

    4.

    新建两个类,

     1 package redis;
     2 
     3 import redis.clients.jedis.JedisPool;
     4 import redis.clients.jedis.JedisPoolConfig;
     5 
     6 /**
     7  * 强指定redis的JedisPool接口构造函数,这样才能在centos成功创建jedispool
     8  *
     9  * @author xiangze
    10  *
    11  */
    12 public class JedisPoolWriper {
    13     //连接池对象
    14     private JedisPool jedisPool;
    15 
    16     public JedisPoolWriper(final JedisPoolConfig poolConfig, final String host,
    17                            final int port ,final int timeout ,final String password) {
    18         try {
    19             //通过连接池配置信息,IP,端口构造连接池对象
    20             jedisPool = new JedisPool(poolConfig, host, port,timeout,password);
    21         } catch (Exception e) {
    22             e.printStackTrace();
    23         }
    24     }
    25     //获取redis连接池对象
    26     public JedisPool getJedisPool() {
    27         return jedisPool;
    28     }
    29     //注入redis连接池对象
    30     public void setJedisPool(JedisPool jedisPool) {
    31         this.jedisPool = jedisPool;
    32     }
    33 
    34 }
    JedisPoolWriper
       1 package redis;
       2 
       3 
       4 
       5 import java.util.List;
       6 import java.util.Map;
       7 import java.util.Set;
       8 
       9 import redis.clients.jedis.Jedis;
      10 import redis.clients.jedis.JedisPool;
      11 import redis.clients.jedis.SortingParams;
      12 
      13 import redis.clients.jedis.BinaryClient.LIST_POSITION;
      14 import redis.clients.util.SafeEncoder;
      15 
      16 /*
      17 下面这两个在高版本的redis.clients依赖包里面没有,需要降低版本才有,如2.9.0
      18  *import redis.clients.jedis.BinaryClient.LIST_POSITION;
      19 import redis.clients.util.SafeEncoder;
      20  */
      21 
      22 public class JedisUtil {
      23     /**
      24      * 缓存生存时间
      25      */
      26     private final int expire = 60000;
      27     /** 操作Key的方法 */
      28     public Keys KEYS;
      29     /** 对存储结构为Set类型的操作 */
      30     public Sets SETS;
      31     /** 对存储结构为HashMap类型的操作 */
      32     public Hash HASH;
      33     /** 对存储结构为String类型的操作 */
      34     public Strings STRINGS;
      35     /** 对存储结构为List类型的操作 */
      36     public Lists LISTS;
      37 
      38 
      39 
      40     private JedisPool jedisPool;
      41 
      42     public JedisPool getJedisPool() {
      43         return jedisPool;
      44     }
      45 
      46     public void setJedisPool(JedisPoolWriper jedisPoolWriper) {
      47         this.jedisPool = jedisPoolWriper.getJedisPool();
      48     }
      49 
      50     public JedisPool getPool() {
      51         return jedisPool;
      52     }
      53 
      54     /**
      55      * 从jedis连接池中获取获取jedis对象
      56      *
      57      * @return
      58      */
      59     public Jedis getJedis() {
      60         return jedisPool.getResource();
      61     }
      62 
      63     /**
      64      * 设置过期时间
      65      *
      66      * @author ruan 2013-4-11
      67      * @param key
      68      * @param seconds
      69      */
      70     public void expire(String key, int seconds) {
      71         if (seconds <= 0) {
      72             return;
      73         }
      74         Jedis jedis = getJedis();
      75         jedis.expire(key, seconds);
      76         jedis.close();
      77     }
      78 
      79     /**
      80      * 设置默认过期时间
      81      *
      82      * @author ruan 2013-4-11
      83      * @param key
      84      */
      85     public void expire(String key) {
      86         expire(key, expire);
      87     }
      88 
      89     // *******************************************Keys*******************************************//
      90     public class Keys {
      91 
      92 
      93         /**
      94          * 清空所有key
      95          */
      96         public String flushAll() {
      97             Jedis jedis = getJedis();
      98             String stata = jedis.flushAll();
      99             jedis.close();
     100             return stata;
     101         }
     102 
     103         /**
     104          * 更改key
     105          *
     106 //         * @param String
     107          *            oldkey
     108 //         * @param String
     109          *            newkey
     110          * @return 状态码
     111          * */
     112         public String rename(String oldkey, String newkey) {
     113             return rename(SafeEncoder.encode(oldkey),
     114                     SafeEncoder.encode(newkey));
     115         }
     116 
     117         /**
     118          * 更改key,仅当新key不存在时才执行
     119          *
     120 //         * @param String
     121          *            oldkey
     122 //         * @param String
     123          *            newkey
     124          * @return 状态码
     125          * */
     126         public long renamenx(String oldkey, String newkey) {
     127             Jedis jedis = getJedis();
     128             long status = jedis.renamenx(oldkey, newkey);
     129             jedis.close();
     130             return status;
     131         }
     132 
     133         /**
     134          * 更改key
     135          *
     136 //         * @param String
     137          *            oldkey
     138 //         * @param String
     139          *            newkey
     140          * @return 状态码
     141          * */
     142         public String rename(byte[] oldkey, byte[] newkey) {
     143             Jedis jedis = getJedis();
     144             String status = jedis.rename(oldkey, newkey);
     145             jedis.close();
     146             return status;
     147         }
     148 
     149         /**
     150          * 设置key的过期时间,以秒为单位
     151          *
     152 //         * @param String
     153          *            key
     154 //         * @param 时间
     155          *            ,已秒为单位
     156          * @return 影响的记录数
     157          * */
     158         public long expired(String key, int seconds) {
     159             Jedis jedis = getJedis();
     160             long count = jedis.expire(key, seconds);
     161             jedis.close();
     162             return count;
     163         }
     164 
     165         /**
     166          * 设置key的过期时间,它是距历元(即格林威治标准时间 1970 年 1 月 1 日的 00:00:00,格里高利历)的偏移量。
     167          *
     168 //         * @param String
     169          *            key
     170 //         * @param 时间
     171          *            ,已秒为单位
     172          * @return 影响的记录数
     173          * */
     174         public long expireAt(String key, long timestamp) {
     175             Jedis jedis = getJedis();
     176             long count = jedis.expireAt(key, timestamp);
     177             jedis.close();
     178             return count;
     179         }
     180 
     181         /**
     182          * 查询key的过期时间
     183          *
     184 //         * @param String
     185          *            key
     186          * @return 以秒为单位的时间表示
     187          * */
     188         public long ttl(String key) {
     189             // ShardedJedis sjedis = getShardedJedis();
     190             Jedis sjedis = getJedis();
     191             long len = sjedis.ttl(key);
     192             sjedis.close();
     193             return len;
     194         }
     195 
     196         /**
     197          * 取消对key过期时间的设置
     198          *
     199          * @param key
     200          * @return 影响的记录数
     201          * */
     202         public long persist(String key) {
     203             Jedis jedis = getJedis();
     204             long count = jedis.persist(key);
     205             jedis.close();
     206             return count;
     207         }
     208 
     209         /**
     210          * 删除keys对应的记录,可以是多个key
     211          *
     212 //         * @param String
     213          *            ... keys
     214          * @return 删除的记录数
     215          * */
     216         public long del(String... keys) {
     217             Jedis jedis = getJedis();
     218             long count = jedis.del(keys);
     219             jedis.close();
     220             return count;
     221         }
     222 
     223 //        String…是java5新加入的功能,表示的是一个可变长度的参数列表。
     224 //        其语法就是类型后跟…,表示此处接受的参数为0到多个Object类型的对象,或者是一个Object[]。
     225 //        例如我们有一个方法叫做test(String…strings),那么你还可以写方法test(),但你不能写test(String[] strings),
     226 //        这样会出编译错误,系统提示出现重复的方法。
     227 //        在使用的时候,对于test(String…strings),你可以直接用test()去调用,标示没有参数,也可以用去test(“aaa”),
     228 //        也可以用test(new String[]{“aaa”,”bbb”})。
     229 //        另外如果既有test(String…strings)函数,又有test()函数,我们在调用test()时,会优先使用test()函数。
     230 //        只有当没有test()函数式,我们调用test(),程序才会走test(String…strings)。
     231 
     232 
     233         /**
     234          * 删除keys对应的记录,可以是多个key
     235          *
     236 //         * @param String
     237          *            ... keys
     238          * @return 删除的记录数
     239          * */
     240         public long del(byte[]... keys) {
     241             Jedis jedis = getJedis();
     242             long count = jedis.del(keys);
     243             jedis.close();
     244             return count;
     245         }
     246 
     247         /**
     248          * 判断key是否存在
     249          *
     250 //         * @param String
     251          *            key
     252          * @return boolean
     253          * */
     254         public boolean exists(String key) {
     255             // ShardedJedis sjedis = getShardedJedis();
     256             Jedis sjedis = getJedis();
     257             boolean exis = sjedis.exists(key);
     258             sjedis.close();
     259             return exis;
     260         }
     261 
     262         /**
     263          * 对List,Set,SortSet进行排序,如果集合数据较大应避免使用这个方法
     264          *
     265 //         * @param String
     266          *            key
     267          * @return List<String> 集合的全部记录
     268          * **/
     269         public List<String> sort(String key) {
     270             // ShardedJedis sjedis = getShardedJedis();
     271             Jedis sjedis = getJedis();
     272             List<String> list = sjedis.sort(key);
     273             sjedis.close();
     274             return list;
     275         }
     276 
     277         /**
     278          * 对List,Set,SortSet进行排序或limit
     279          *
     280 //         * @param String
     281          *            key
     282 //         * @param SortingParams
     283          *            parame 定义排序类型或limit的起止位置.
     284          * @return List<String> 全部或部分记录
     285          * **/
     286         public List<String> sort(String key, SortingParams parame) {
     287             // ShardedJedis sjedis = getShardedJedis();
     288             Jedis sjedis = getJedis();
     289             List<String> list = sjedis.sort(key, parame);
     290             sjedis.close();
     291             return list;
     292         }
     293 
     294         /**
     295          * 返回指定key存储的类型
     296          *
     297 //         * @param String
     298          *            key
     299          * @return String string|list|set|zset|hash
     300          * **/
     301         public String type(String key) {
     302             // ShardedJedis sjedis = getShardedJedis();
     303             Jedis sjedis = getJedis();
     304             String type = sjedis.type(key);
     305             sjedis.close();
     306             return type;
     307         }
     308 
     309         /**
     310          * 查找所有匹配给定的模式的键
     311          *
     312 //         * @param String
     313          *            key的表达式,*表示多个,?表示一个
     314          * */
     315         public Set<String> keys(String pattern) {
     316             Jedis jedis = getJedis();
     317             Set<String> set = jedis.keys(pattern);
     318             jedis.close();
     319             return set;
     320         }
     321     }
     322 
     323     // *******************************************Sets*******************************************//
     324     public class Sets {
     325 
     326 
     327         /**
     328          * 向Set添加一条记录,如果member已存在返回0,否则返回1
     329          *
     330 //         * @param String
     331          *            key
     332 //         * @param String
     333          *            member
     334          * @return 操作码,0或1
     335          * */
     336         public long sadd(String key, String member) {
     337             Jedis jedis = getJedis();
     338             long s = jedis.sadd(key, member);
     339             jedis.close();
     340             return s;
     341         }
     342 
     343         public long sadd(byte[] key, byte[] member) {
     344             Jedis jedis = getJedis();
     345             long s = jedis.sadd(key, member);
     346             jedis.close();
     347             return s;
     348         }
     349 
     350         /**
     351          * 获取给定key中元素个数
     352          *
     353 //         * @param String
     354          *            key
     355          * @return 元素个数
     356          * */
     357         public long scard(String key) {
     358             // ShardedJedis sjedis = getShardedJedis();
     359             Jedis sjedis = getJedis();
     360             long len = sjedis.scard(key);
     361             sjedis.close();
     362             return len;
     363         }
     364 
     365         /**
     366          * 返回从第一组和所有的给定集合之间的差异的成员
     367          *
     368 //         * @param String
     369          *            ... keys
     370          * @return 差异的成员集合
     371          * */
     372         public Set<String> sdiff(String... keys) {
     373             Jedis jedis = getJedis();
     374             Set<String> set = jedis.sdiff(keys);
     375             jedis.close();
     376             return set;
     377         }
     378 
     379         /**
     380          * 这个命令等于sdiff,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
     381          *
     382 //         * @param String
     383          *            newkey 新结果集的key
     384 //         * @param String
     385          *            ... keys 比较的集合
     386          * @return 新集合中的记录数
     387          * **/
     388         public long sdiffstore(String newkey, String... keys) {
     389             Jedis jedis = getJedis();
     390             long s = jedis.sdiffstore(newkey, keys);
     391             jedis.close();
     392             return s;
     393         }
     394 
     395         /**
     396          * 返回给定集合交集的成员,如果其中一个集合为不存在或为空,则返回空Set
     397          *
     398 //         * @param String
     399          *            ... keys
     400          * @return 交集成员的集合
     401          * **/
     402         public Set<String> sinter(String... keys) {
     403             Jedis jedis = getJedis();
     404             Set<String> set = jedis.sinter(keys);
     405             jedis.close();
     406             return set;
     407         }
     408 
     409         /**
     410          * 这个命令等于sinter,但返回的不是结果集,而是将结果集存储在新的集合中,如果目标已存在,则覆盖。
     411          *
     412 //         * @param String
     413          *            newkey 新结果集的key
     414 //         * @param String
     415          *            ... keys 比较的集合
     416          * @return 新集合中的记录数
     417          * **/
     418         public long sinterstore(String newkey, String... keys) {
     419             Jedis jedis = getJedis();
     420             long s = jedis.sinterstore(newkey, keys);
     421             jedis.close();
     422             return s;
     423         }
     424 
     425         /**
     426          * 确定一个给定的值是否存在
     427          *
     428 //         * @param String
     429          *            key
     430 //         * @param String
     431          *            member 要判断的值
     432          * @return 存在返回1,不存在返回0
     433          * **/
     434         public boolean sismember(String key, String member) {
     435             // ShardedJedis sjedis = getShardedJedis();
     436             Jedis sjedis = getJedis();
     437             boolean s = sjedis.sismember(key, member);
     438             sjedis.close();
     439             return s;
     440         }
     441 
     442         /**
     443          * 返回集合中的所有成员
     444          *
     445 //         * @param String
     446          *            key
     447          * @return 成员集合
     448          * */
     449         public Set<String> smembers(String key) {
     450             // ShardedJedis sjedis = getShardedJedis();
     451             Jedis sjedis = getJedis();
     452             Set<String> set = sjedis.smembers(key);
     453             sjedis.close();
     454             return set;
     455         }
     456 
     457         public Set<byte[]> smembers(byte[] key) {
     458             // ShardedJedis sjedis = getShardedJedis();
     459             Jedis sjedis = getJedis();
     460             Set<byte[]> set = sjedis.smembers(key);
     461             sjedis.close();
     462             return set;
     463         }
     464 
     465         /**
     466          * 将成员从源集合移出放入目标集合 <br/>
     467          * 如果源集合不存在或不包哈指定成员,不进行任何操作,返回0<br/>
     468          * 否则该成员从源集合上删除,并添加到目标集合,如果目标集合中成员已存在,则只在源集合进行删除
     469          *
     470 //         * @param String
     471          *            srckey 源集合
     472 //         * @param String
     473          *            dstkey 目标集合
     474 //         * @param String
     475          *            member 源集合中的成员
     476          * @return 状态码,1成功,0失败
     477          * */
     478         public long smove(String srckey, String dstkey, String member) {
     479             Jedis jedis = getJedis();
     480             long s = jedis.smove(srckey, dstkey, member);
     481             jedis.close();
     482             return s;
     483         }
     484 
     485         /**
     486          * 从集合中删除成员
     487          *
     488 //         * @param String
     489          *            key
     490          * @return 被删除的成员
     491          * */
     492         public String spop(String key) {
     493             Jedis jedis = getJedis();
     494             String s = jedis.spop(key);
     495             jedis.close();
     496             return s;
     497         }
     498 
     499         /**
     500          * 从集合中删除指定成员
     501          *
     502 //         * @param String
     503          *            key
     504 //         * @param String
     505          *            member 要删除的成员
     506          * @return 状态码,成功返回1,成员不存在返回0
     507          * */
     508         public long srem(String key, String member) {
     509             Jedis jedis = getJedis();
     510             long s = jedis.srem(key, member);
     511             jedis.close();
     512             return s;
     513         }
     514 
     515         /**
     516          * 合并多个集合并返回合并后的结果,合并后的结果集合并不保存<br/>
     517          *
     518 //         * @param String
     519          *            ... keys
     520          * @return 合并后的结果集合
     521 //         * @see sunionstore
     522          * */
     523         public Set<String> sunion(String... keys) {
     524             Jedis jedis = getJedis();
     525             Set<String> set = jedis.sunion(keys);
     526             jedis.close();
     527             return set;
     528         }
     529 
     530         /**
     531          * 合并多个集合并将合并后的结果集保存在指定的新集合中,如果新集合已经存在则覆盖
     532          *
     533 //         * @param String
     534          *            newkey 新集合的key
     535 //         * @param String
     536          *            ... keys 要合并的集合
     537          * **/
     538         public long sunionstore(String newkey, String... keys) {
     539             Jedis jedis = getJedis();
     540             long s = jedis.sunionstore(newkey, keys);
     541             jedis.close();
     542             return s;
     543         }
     544     }
     545 
     546     // *******************************************Hash*******************************************//
     547     public class Hash {
     548 
     549         /**
     550          * 从hash中删除指定的存储
     551          *
     552 //         * @param String
     553          *            key
     554 //         * @param String
     555          *            fieid 存储的名字
     556          * @return 状态码,1成功,0失败
     557          * */
     558         public long hdel(String key, String fieid) {
     559             Jedis jedis = getJedis();
     560             long s = jedis.hdel(key, fieid);
     561             jedis.close();
     562             return s;
     563         }
     564 
     565         public long hdel(String key) {
     566             Jedis jedis = getJedis();
     567             long s = jedis.del(key);
     568             jedis.close();
     569             return s;
     570         }
     571 
     572         /**
     573          * 测试hash中指定的存储是否存在
     574          *
     575 //         * @param String
     576          *            key
     577 //         * @param String
     578          *            fieid 存储的名字
     579          * @return 1存在,0不存在
     580          * */
     581         public boolean hexists(String key, String fieid) {
     582             // ShardedJedis sjedis = getShardedJedis();
     583             Jedis sjedis = getJedis();
     584             boolean s = sjedis.hexists(key, fieid);
     585             sjedis.close();
     586             return s;
     587         }
     588 
     589         /**
     590          * 返回hash中指定存储位置的值
     591          *
     592 //         * @param String
     593          *            key
     594 //         * @param String
     595          *            fieid 存储的名字
     596          * @return 存储对应的值
     597          * */
     598         public String hget(String key, String fieid) {
     599             // ShardedJedis sjedis = getShardedJedis();
     600             Jedis sjedis = getJedis();
     601             String s = sjedis.hget(key, fieid);
     602             sjedis.close();
     603             return s;
     604         }
     605 
     606         public byte[] hget(byte[] key, byte[] fieid) {
     607             // ShardedJedis sjedis = getShardedJedis();
     608             Jedis sjedis = getJedis();
     609             byte[] s = sjedis.hget(key, fieid);
     610             sjedis.close();
     611             return s;
     612         }
     613 
     614         /**
     615          * 以Map的形式返回hash中的存储和值
     616          *
     617 //         * @param String
     618          *            key
     619          * @return Map<Strinig,String>
     620          * */
     621         public Map<String, String> hgetAll(String key) {
     622             // ShardedJedis sjedis = getShardedJedis();
     623             Jedis sjedis = getJedis();
     624             Map<String, String> map = sjedis.hgetAll(key);
     625             sjedis.close();
     626             return map;
     627         }
     628 
     629         /**
     630          * 添加一个对应关系
     631          *
     632 //         * @param String
     633          *            key
     634 //         * @param String
     635          *            fieid
     636 //         * @param String
     637          *            value
     638          * @return 状态码 1成功,0失败,fieid已存在将更新,也返回0
     639          * **/
     640         public long hset(String key, String fieid, String value) {
     641             Jedis jedis = getJedis();
     642             long s = jedis.hset(key, fieid, value);
     643             jedis.close();
     644             return s;
     645         }
     646 
     647         public long hset(String key, String fieid, byte[] value) {
     648             Jedis jedis = getJedis();
     649             long s = jedis.hset(key.getBytes(), fieid.getBytes(), value);
     650             jedis.close();
     651             return s;
     652         }
     653 
     654         /**
     655          * 添加对应关系,只有在fieid不存在时才执行
     656          *
     657 //         * @param String
     658          *            key
     659 //         * @param String
     660          *            fieid
     661 //         * @param String
     662          *            value
     663          * @return 状态码 1成功,0失败fieid已存
     664          * **/
     665         public long hsetnx(String key, String fieid, String value) {
     666             Jedis jedis = getJedis();
     667             long s = jedis.hsetnx(key, fieid, value);
     668             jedis.close();
     669             return s;
     670         }
     671 
     672         /**
     673          * 获取hash中value的集合
     674          *
     675 //         * @param String
     676          *            key
     677          * @return List<String>
     678          * */
     679         public List<String> hvals(String key) {
     680             // ShardedJedis sjedis = getShardedJedis();
     681             Jedis sjedis = getJedis();
     682             List<String> list = sjedis.hvals(key);
     683             sjedis.close();
     684             return list;
     685         }
     686 
     687         /**
     688          * 在指定的存储位置加上指定的数字,存储位置的值必须可转为数字类型
     689          *
     690 //         * @param String
     691          *            key
     692 //         * @param String
     693          *            fieid 存储位置
     694 //         * @param String
     695          *            long value 要增加的值,可以是负数
     696          * @return 增加指定数字后,存储位置的值
     697          * */
     698         public long hincrby(String key, String fieid, long value) {
     699             Jedis jedis = getJedis();
     700             long s = jedis.hincrBy(key, fieid, value);
     701             jedis.close();
     702             return s;
     703         }
     704 
     705         /**
     706          * 返回指定hash中的所有存储名字,类似Map中的keySet方法
     707          *
     708 //         * @param String
     709          *            key
     710          * @return Set<String> 存储名称的集合
     711          * */
     712         public Set<String> hkeys(String key) {
     713             // ShardedJedis sjedis = getShardedJedis();
     714             Jedis sjedis = getJedis();
     715             Set<String> set = sjedis.hkeys(key);
     716             sjedis.close();
     717             return set;
     718         }
     719 
     720         /**
     721          * 获取hash中存储的个数,类似Map中size方法
     722          *
     723 //         * @param String
     724          *            key
     725          * @return long 存储的个数
     726          * */
     727         public long hlen(String key) {
     728             // ShardedJedis sjedis = getShardedJedis();
     729             Jedis sjedis = getJedis();
     730             long len = sjedis.hlen(key);
     731             sjedis.close();
     732             return len;
     733         }
     734 
     735         /**
     736          * 根据多个key,获取对应的value,返回List,如果指定的key不存在,List对应位置为null
     737          *
     738 //         * @param String
     739          *            key
     740 //         * @param String
     741          *            ... fieids 存储位置
     742          * @return List<String>
     743          * */
     744         public List<String> hmget(String key, String... fieids) {
     745             // ShardedJedis sjedis = getShardedJedis();
     746             Jedis sjedis = getJedis();
     747             List<String> list = sjedis.hmget(key, fieids);
     748             sjedis.close();
     749             return list;
     750         }
     751 
     752         public List<byte[]> hmget(byte[] key, byte[]... fieids) {
     753             // ShardedJedis sjedis = getShardedJedis();
     754             Jedis sjedis = getJedis();
     755             List<byte[]> list = sjedis.hmget(key, fieids);
     756             sjedis.close();
     757             return list;
     758         }
     759 
     760         /**
     761          * 添加对应关系,如果对应关系已存在,则覆盖
     762          *
     763 //         * @param Strin
     764          *            key
     765 //         * @param Map
     766          *            <String,String> 对应关系
     767          * @return 状态,成功返回OK
     768          * */
     769         public String hmset(String key, Map<String, String> map) {
     770             Jedis jedis = getJedis();
     771             String s = jedis.hmset(key, map);
     772             jedis.close();
     773             return s;
     774         }
     775 
     776         /**
     777          * 添加对应关系,如果对应关系已存在,则覆盖
     778          *
     779 //         * @param Strin
     780          *            key
     781 //         * @param Map
     782          *            <String,String> 对应关系
     783          * @return 状态,成功返回OK
     784          * */
     785         public String hmset(byte[] key, Map<byte[], byte[]> map) {
     786             Jedis jedis = getJedis();
     787             String s = jedis.hmset(key, map);
     788             jedis.close();
     789             return s;
     790         }
     791 
     792     }
     793 
     794     // *******************************************Strings*******************************************//
     795     public class Strings {
     796 
     797 
     798 
     799         /**
     800          * 根据key获取记录
     801          *
     802 //         * @param String
     803          *            key
     804          * @return 805          * */
     806         public String get(String key) {
     807             // ShardedJedis sjedis = getShardedJedis();
     808             Jedis sjedis = getJedis();
     809             String value = sjedis.get(key);
     810             sjedis.close();
     811             return value;
     812         }
     813 
     814         /**
     815          * 根据key获取记录
     816          *
     817 //         * @param byte[] key
     818          * @return 819          * */
     820         public byte[] get(byte[] key) {
     821             // ShardedJedis sjedis = getShardedJedis();
     822             Jedis sjedis = getJedis();
     823             byte[] value = sjedis.get(key);
     824             sjedis.close();
     825             return value;
     826         }
     827 
     828         /**
     829          * 添加有过期时间的记录
     830          *
     831 //         * @param String
     832          *            key
     833 //         * @param int seconds 过期时间,以秒为单位
     834 //         * @param String
     835          *            value
     836          * @return String 操作状态
     837          * */
     838         public String setEx(String key, int seconds, String value) {
     839             Jedis jedis = getJedis();
     840             String str = jedis.setex(key, seconds, value);
     841             jedis.close();
     842             return str;
     843         }
     844 
     845         /**
     846          * 添加有过期时间的记录
     847          *
     848 //         * @param String
     849          *            key
     850 //         * @param int seconds 过期时间,以秒为单位
     851 //         * @param String
     852          *            value
     853          * @return String 操作状态
     854          * */
     855         public String setEx(byte[] key, int seconds, byte[] value) {
     856             Jedis jedis = getJedis();
     857             String str = jedis.setex(key, seconds, value);
     858             jedis.close();
     859             return str;
     860         }
     861 
     862         /**
     863          * 添加一条记录,仅当给定的key不存在时才插入
     864          *
     865 //         * @param String
     866          *            key
     867 //         * @param String
     868          *            value
     869          * @return long 状态码,1插入成功且key不存在,0未插入,key存在
     870          * */
     871         public long setnx(String key, String value) {
     872             Jedis jedis = getJedis();
     873             long str = jedis.setnx(key, value);
     874             jedis.close();
     875             return str;
     876         }
     877 
     878         /**
     879          * 添加记录,如果记录已存在将覆盖原有的value
     880          *
     881 //         * @param String
     882          *            key
     883 //         * @param String
     884          *            value
     885          * @return 状态码
     886          * */
     887         public String set(String key, String value) {
     888             return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
     889         }
     890 
     891         /**
     892          * 添加记录,如果记录已存在将覆盖原有的value
     893          *
     894 //         * @param String
     895          *            key
     896 //         * @param String
     897          *            value
     898          * @return 状态码
     899          * */
     900         public String set(String key, byte[] value) {
     901             return set(SafeEncoder.encode(key), value);
     902         }
     903 
     904         /**
     905          * 添加记录,如果记录已存在将覆盖原有的value
     906          *
     907 //         * @param byte[] key
     908 //         * @param byte[] value
     909          * @return 状态码
     910          * */
     911         public String set(byte[] key, byte[] value) {
     912             Jedis jedis = getJedis();
     913             String status = jedis.set(key, value);
     914             jedis.close();
     915             return status;
     916         }
     917 
     918         /**
     919          * 从指定位置开始插入数据,插入的数据会覆盖指定位置以后的数据<br/>
     920          * 例:String str1="123456789";<br/>
     921          * 对str1操作后setRange(key,4,0000),str1="123400009";
     922          *
     923 //         * @param String
     924 //         *            key
     925 //         * @param long offset
     926 //         * @param String
     927          *            value
     928          * @return long value的长度
     929          * */
     930         public long setRange(String key, long offset, String value) {
     931             Jedis jedis = getJedis();
     932             long len = jedis.setrange(key, offset, value);
     933             jedis.close();
     934             return len;
     935         }
     936 
     937         /**
     938          * 在指定的key中追加value
     939          *
     940 //         * @param String
     941 //         *            key
     942 //         * @param String
     943          *            value
     944          * @return long 追加后value的长度
     945          * **/
     946         public long append(String key, String value) {
     947             Jedis jedis = getJedis();
     948             long len = jedis.append(key, value);
     949             jedis.close();
     950             return len;
     951         }
     952 
     953         /**
     954          * 将key对应的value减去指定的值,只有value可以转为数字时该方法才可用
     955          *
     956 //         * @param String
     957 //         *            key
     958 //         * @param long number 要减去的值
     959          * @return long 减指定值后的值
     960          * */
     961         public long decrBy(String key, long number) {
     962             Jedis jedis = getJedis();
     963             long len = jedis.decrBy(key, number);
     964             jedis.close();
     965             return len;
     966         }
     967 
     968         /**
     969          * <b>可以作为获取唯一id的方法</b><br/>
     970          * 将key对应的value加上指定的值,只有value可以转为数字时该方法才可用
     971          *
     972 //         * @param String
     973 //         *            key
     974 //         * @param long number 要减去的值
     975          * @return long 相加后的值
     976          * */
     977         public long incrBy(String key, long number) {
     978             Jedis jedis = getJedis();
     979             long len = jedis.incrBy(key, number);
     980             jedis.close();
     981             return len;
     982         }
     983 
     984         /**
     985          * 对指定key对应的value进行截取
     986          *
     987 //         * @param String
     988 //         *            key
     989 //         * @param long startOffset 开始位置(包含)
     990 //         * @param long endOffset 结束位置(包含)
     991          * @return String 截取的值
     992          * */
     993         public String getrange(String key, long startOffset, long endOffset) {
     994             // ShardedJedis sjedis = getShardedJedis();
     995             Jedis sjedis = getJedis();
     996             String value = sjedis.getrange(key, startOffset, endOffset);
     997             sjedis.close();
     998             return value;
     999         }
    1000 
    1001         /**
    1002          * 获取并设置指定key对应的value<br/>
    1003          * 如果key存在返回之前的value,否则返回null
    1004          *
    1005 //         * @param String
    1006 //         *            key
    1007 //         * @param String
    1008          *            value
    1009          * @return String 原始value或null
    1010          * */
    1011         public String getSet(String key, String value) {
    1012             Jedis jedis = getJedis();
    1013             String str = jedis.getSet(key, value);
    1014             jedis.close();
    1015             return str;
    1016         }
    1017 
    1018         /**
    1019          * 批量获取记录,如果指定的key不存在返回List的对应位置将是null
    1020          *
    1021 //         * @param String
    1022          *            keys
    1023          * @return List<String> 值得集合
    1024          * */
    1025         public List<String> mget(String... keys) {
    1026             Jedis jedis = getJedis();
    1027             List<String> str = jedis.mget(keys);
    1028             jedis.close();
    1029             return str;
    1030         }
    1031 
    1032         /**
    1033          * 批量存储记录
    1034          *
    1035 //         * @param String
    1036          *            keysvalues 例:keysvalues="key1","value1","key2","value2";
    1037          * @return String 状态码
    1038          * */
    1039         public String mset(String... keysvalues) {
    1040             Jedis jedis = getJedis();
    1041             String str = jedis.mset(keysvalues);
    1042             jedis.close();
    1043             return str;
    1044         }
    1045 
    1046         /**
    1047          * 获取key对应的值的长度
    1048          *
    1049 //         * @param String
    1050          *            key
    1051          * @return value值得长度
    1052          * */
    1053         public long strlen(String key) {
    1054             Jedis jedis = getJedis();
    1055             long len = jedis.strlen(key);
    1056             jedis.close();
    1057             return len;
    1058         }
    1059     }
    1060 
    1061     // *******************************************Lists*******************************************//
    1062     public class Lists {
    1063 
    1064 
    1065         /**
    1066          * List长度
    1067          *
    1068 //         * @param String
    1069          *            key
    1070          * @return 长度
    1071          * */
    1072         public long llen(String key) {
    1073             return llen(SafeEncoder.encode(key));
    1074         }
    1075 
    1076         /**
    1077          * List长度
    1078          *
    1079 //         * @param byte[] key
    1080          * @return 长度
    1081          * */
    1082         public long llen(byte[] key) {
    1083             // ShardedJedis sjedis = getShardedJedis();
    1084             Jedis sjedis = getJedis();
    1085             long count = sjedis.llen(key);
    1086             sjedis.close();
    1087             return count;
    1088         }
    1089 
    1090         /**
    1091          * 覆盖操作,将覆盖List中指定位置的值
    1092          *
    1093 //         * @param byte[] key
    1094 //         * @param int index 位置
    1095 //         * @param byte[] value 值
    1096          * @return 状态码
    1097          * */
    1098         public String lset(byte[] key, int index, byte[] value) {
    1099             Jedis jedis = getJedis();
    1100             String status = jedis.lset(key, index, value);
    1101             jedis.close();
    1102             return status;
    1103         }
    1104 
    1105         /**
    1106          * 覆盖操作,将覆盖List中指定位置的值
    1107          *
    1108 //         * @param key
    1109 //         * @param int index 位置
    1110 //         * @param String
    1111          *            value 值
    1112          * @return 状态码
    1113          * */
    1114         public String lset(String key, int index, String value) {
    1115             return lset(SafeEncoder.encode(key), index,
    1116                     SafeEncoder.encode(value));
    1117         }
    1118 
    1119         /**
    1120          * 在value的相对位置插入记录
    1121          *
    1122 //         * @param key
    1123 //         * @param LIST_POSITION
    1124 //         *            前面插入或后面插入
    1125 //         * @param String
    1126 //         *            pivot 相对位置的内容
    1127 //         * @param String
    1128 //         *            value 插入的内容
    1129          * @return 记录总数
    1130          * */
    1131         public long linsert(String key, LIST_POSITION where, String pivot,
    1132                             String value) {
    1133             return linsert(SafeEncoder.encode(key), where,
    1134                     SafeEncoder.encode(pivot), SafeEncoder.encode(value));
    1135         }
    1136 
    1137         /**
    1138          * 在指定位置插入记录
    1139          *
    1140 //         * @param String
    1141 //         *            key
    1142 //         * @param LIST_POSITION
    1143 //         *            前面插入或后面插入
    1144 //         * @param byte[] pivot 相对位置的内容
    1145 //         * @param byte[] value 插入的内容
    1146          * @return 记录总数
    1147          * */
    1148         public long linsert(byte[] key, LIST_POSITION where, byte[] pivot,
    1149                             byte[] value) {
    1150             Jedis jedis = getJedis();
    1151             long count = jedis.linsert(key, where, pivot, value);
    1152             jedis.close();
    1153             return count;
    1154         }
    1155 
    1156         /**
    1157          * 获取List中指定位置的值
    1158          *
    1159 //         * @param String
    1160 //         *            key
    1161 //         * @param int index 位置
    1162          * @return1163          * **/
    1164         public String lindex(String key, int index) {
    1165             return SafeEncoder.encode(lindex(SafeEncoder.encode(key), index));
    1166         }
    1167 
    1168         /**
    1169          * 获取List中指定位置的值
    1170          *
    1171 //         * @param byte[] key
    1172 //         * @param int index 位置
    1173 //         * @return1174          * **/
    1175         public byte[] lindex(byte[] key, int index) {
    1176             // ShardedJedis sjedis = getShardedJedis();
    1177             Jedis sjedis = getJedis();
    1178             byte[] value = sjedis.lindex(key, index);
    1179             sjedis.close();
    1180             return value;
    1181         }
    1182 
    1183         /**
    1184          * 将List中的第一条记录移出List
    1185          *
    1186 //         * @param String
    1187          *            key
    1188          * @return 移出的记录
    1189          * */
    1190         public String lpop(String key) {
    1191             return SafeEncoder.encode(lpop(SafeEncoder.encode(key)));
    1192         }
    1193 
    1194         /**
    1195          * 将List中的第一条记录移出List
    1196          *
    1197 //         * @param byte[] key
    1198          * @return 移出的记录
    1199          * */
    1200         public byte[] lpop(byte[] key) {
    1201             Jedis jedis = getJedis();
    1202             byte[] value = jedis.lpop(key);
    1203             jedis.close();
    1204             return value;
    1205         }
    1206 
    1207         /**
    1208          * 将List中最后第一条记录移出List
    1209          *
    1210 //         * @param byte[] key
    1211          * @return 移出的记录
    1212          * */
    1213         public String rpop(String key) {
    1214             Jedis jedis = getJedis();
    1215             String value = jedis.rpop(key);
    1216             jedis.close();
    1217             return value;
    1218         }
    1219 
    1220         /**
    1221          * 向List尾部追加记录
    1222          *
    1223 //         * @param String
    1224 //         *            key
    1225 //         * @param String
    1226          *            value
    1227          * @return 记录总数
    1228          * */
    1229         public long lpush(String key, String value) {
    1230             return lpush(SafeEncoder.encode(key), SafeEncoder.encode(value));
    1231         }
    1232 
    1233         /**
    1234          * 向List头部追加记录
    1235          *
    1236 //         * @param String
    1237 //         *            key
    1238 //         * @param String
    1239          *            value
    1240          * @return 记录总数
    1241          * */
    1242         public long rpush(String key, String value) {
    1243             Jedis jedis = getJedis();
    1244             long count = jedis.rpush(key, value);
    1245             jedis.close();
    1246             return count;
    1247         }
    1248 
    1249         /**
    1250          * 向List头部追加记录
    1251          *
    1252 //         * @param String
    1253 //         *            key
    1254 //         * @param String
    1255          *            value
    1256          * @return 记录总数
    1257          * */
    1258         public long rpush(byte[] key, byte[] value) {
    1259             Jedis jedis = getJedis();
    1260             long count = jedis.rpush(key, value);
    1261             jedis.close();
    1262             return count;
    1263         }
    1264 
    1265         /**
    1266          * 向List中追加记录
    1267          *
    1268 //         * @param byte[] key
    1269 //         * @param byte[] value
    1270          * @return 记录总数
    1271          * */
    1272         public long lpush(byte[] key, byte[] value) {
    1273             Jedis jedis = getJedis();
    1274             long count = jedis.lpush(key, value);
    1275             jedis.close();
    1276             return count;
    1277         }
    1278 
    1279         /**
    1280          * 获取指定范围的记录,可以做为分页使用
    1281          *
    1282 //         * @param String
    1283 //         *            key
    1284 //         * @param long start
    1285 //         * @param long end
    1286          * @return List
    1287          * */
    1288         public List<String> lrange(String key, long start, long end) {
    1289             // ShardedJedis sjedis = getShardedJedis();
    1290             Jedis sjedis = getJedis();
    1291             List<String> list = sjedis.lrange(key, start, end);
    1292             sjedis.close();
    1293             return list;
    1294         }
    1295 
    1296         /**
    1297          * 获取指定范围的记录,可以做为分页使用
    1298          *
    1299 //         * @param byte[] key
    1300 //         * @param int start
    1301 //         * @param int end 如果为负数,则尾部开始计算
    1302          * @return List
    1303          * */
    1304         public List<byte[]> lrange(byte[] key, int start, int end) {
    1305             // ShardedJedis sjedis = getShardedJedis();
    1306             Jedis sjedis = getJedis();
    1307             List<byte[]> list = sjedis.lrange(key, start, end);
    1308             sjedis.close();
    1309             return list;
    1310         }
    1311 
    1312         /**
    1313          * 删除List中c条记录,被删除的记录值为value
    1314          *
    1315 //         * @param byte[] key
    1316 //         * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
    1317 //         * @param byte[] value 要匹配的值
    1318          * @return 删除后的List中的记录数
    1319          * */
    1320         public long lrem(byte[] key, int c, byte[] value) {
    1321             Jedis jedis = getJedis();
    1322             long count = jedis.lrem(key, c, value);
    1323             jedis.close();
    1324             return count;
    1325         }
    1326 
    1327         /**
    1328          * 删除List中c条记录,被删除的记录值为value
    1329          *
    1330 //         * @param String
    1331 //         *            key
    1332 //         * @param int c 要删除的数量,如果为负数则从List的尾部检查并删除符合的记录
    1333 //         * @param String
    1334          *            value 要匹配的值
    1335          * @return 删除后的List中的记录数
    1336          * */
    1337         public long lrem(String key, int c, String value) {
    1338             return lrem(SafeEncoder.encode(key), c, SafeEncoder.encode(value));
    1339         }
    1340 
    1341         /**
    1342          * 算是删除吧,只保留start与end之间的记录
    1343          *
    1344 //         * @param byte[] key
    1345 //         * @param int start 记录的开始位置(0表示第一条记录)
    1346 //         * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
    1347          * @return 执行状态码
    1348          * */
    1349         public String ltrim(byte[] key, int start, int end) {
    1350             Jedis jedis = getJedis();
    1351             String str = jedis.ltrim(key, start, end);
    1352             jedis.close();
    1353             return str;
    1354         }
    1355 
    1356         /**
    1357          * 算是删除吧,只保留start与end之间的记录
    1358          *
    1359 //         * @param String
    1360 //         *            key
    1361 //         * @param int start 记录的开始位置(0表示第一条记录)
    1362 //         * @param int end 记录的结束位置(如果为-1则表示最后一个,-2,-3以此类推)
    1363          * @return 执行状态码
    1364          * */
    1365         public String ltrim(String key, int start, int end) {
    1366             return ltrim(SafeEncoder.encode(key), start, end);
    1367         }
    1368     }
    1369 
    1370 }
    JedisUtil

    5.

    spring 的 controller层,可以通过@Autowired注入 后直接调用方法,

    但是有缺陷,

    如果redisSpringContext.xml导入dispatcher-servlet.xml中,会导致

    //    @Autowired
    // private JedisUtil.Strings jedisString;
    只能在controller层注入,不可以在service层注入,否则会报错,无法编译,具体原因不知,猜测是因为我把servlet层和service层的配置文件分开设置了,想要
    在service层注入使用,只需要把redisSpringContext.xml导入springcontext-service.xml文件即可,当然,redisSpringContext.xml文件写法有部分改动
    在我的另一篇博客有记载:点击访问

     1 package controller;
     2 
     3 import org.springframework.beans.factory.annotation.Autowired;
     4 import org.springframework.stereotype.Controller;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.ResponseBody;
     7 import redis.JedisUtil;
     8 
     9 
    10 @Controller
    11 public class RedisController {
    12 
    13 
    14     @RequestMapping("/toRedis")
    15     public String toRedis() {
    16         return "redis";
    17     }
    18 
    19 
    20 
    21 
    22 //    /** 操作Key的方法 */
    23 //    public Keys KEYS;
    24 //    /** 对存储结构为String类型的操作 */
    25 //    public Strings STRINGS;
    26 //    /** 对存储结构为List类型的操作 */
    27 //    public Lists LISTS;
    28 //    /** 对存储结构为Set类型的操作 */
    29 //    public Sets SETS;
    30 //    /** 对存储结构为HashMap类型的操作 */
    31 //    public Hash HASH;
    32     @Autowired
    33     private JedisUtil.Keys jedisKeys;
    34 
    35     @Autowired
    36     private JedisUtil.Strings jedisString;
    37 
    38     @RequestMapping("getRedis1")
    39     @ResponseBody
    40     public String getRedis1() {
    41         String str = "";
    42         try{
    43 //            jedisString.set("name","loveyou999"+System.currentTimeMillis());
    44 //            str = jedisString.get("name");
    45 //            System.out.println(str);
    46             long start = System.currentTimeMillis();
    47             for (int i = 0; i < 100000; i++) {
    48                 //返回的是个字符串
    49                 String res = jedisString.set("n" + i, "n" + i);
    50                 System.out.println("返回的结果:" + res);
    51                 //返回的结果:OK
    52             }
    53             long end = System.currentTimeMillis();
    54             System.out.println("普通同步写入:" + ((end - start) / 1000.0) + "秒");
    55         }catch (Exception e){
    56             e.printStackTrace();
    57         }
    58 //        (int)(Math.random() * (end-start+1) + start)
    59         int num = (int)(Math.random() * (99999) + 1);
    60         System.out.println(" num="+num);
    61         //获取字符型键值对
    62         str = jedisString.get("n"+num );
    63         System.out.println(str);
    64         return str;
    65     }
    66 
    67 
    68 }
    View Code


    参考博客原址:https://www.cnblogs.com/lyq-biu/p/10987812.html









  • 相关阅读:
    Codeforces Round #296 (Div. 2B. Error Correct System
    实验十二 图的建立与遍历
    1561: (More) Multiplication
    1562: Fun House
    hdu 2203 亲和串
    hdu 3549Flow Problem
    poj 2182 Lost Cows
    poj 3468A Simple Problem with Integers
    hdu1698 Just a Hook
    栈和队列的面试题Java实现
  • 原文地址:https://www.cnblogs.com/c2g5201314/p/12234649.html
Copyright © 2011-2022 走看看