赞助

Redis缓存的使用

首先需要去Redis官网下载Redis的安装包

 

要在eclipse中使用Redis还需要两个jar包,需要的自行查找或者联系我

 

运行Redis需要开启Redis的服务端,也就是下载的安装包中的“redis-server.exe"

运行中如下图表示运行成功

 

此时就可以在项目中使用Jedis进行操作了

 

 1 package com.llh;
 2 
 3 import java.util.HashMap;
 4 import java.util.HashSet;
 5 
 6 import redis.clients.jedis.Jedis;
 7 
 8 public class JedisTest2 {
 9 
10     public void addHashMap(String key, HashMap map) {
11         Jedis redis = new Jedis("127.0.0.1", 6379);// 连接redis
12         redis.hmset(key, map);
13         System.out.println("hashmap set success!");
14     }
15 
16     public HashSet queryHashMapByKey(String key) {
17         Jedis redis = new Jedis("127.0.0.1", 6379);// 连接redis
18         return (HashSet) redis.hkeys(key);
19     }
20 
21     public String queryString(String key) {
22         Jedis redis = new Jedis("127.0.0.1", 6379);// 连接redis
23         return redis.get(key);
24     };
25 
26     public void addString(String key, String value) {
27         Jedis redis = new Jedis("127.0.0.1", 6379);// 连接redis
28         // redis.auth("redis");//验证密码
29         // 系统中所有key:
30         /*
31          * Set keys = redis.keys("*"); Iterator t1=keys.iterator() ;
32          * while(t1.hasNext()){ Object obj1=t1.next(); System.out.println(obj1);
33          * }
34          */
35         // DEL 移除给定的一个或多个key。如果key不存在,则忽略该命令。
36         // redis.del("key");
37         // expire 设置Key的过期时间(以秒为单位)
38         // redis.expire("key", 5);
39         // TTL 返回给定key的剩余生存时间(time to live)(以秒为单位)
40         // redis.ttl("key");
41 
42         // PERSIST key 移除给定key的生存时间。
43         // redis.persist("key");
44 
45         // EXISTS 检查给定key是否存在。
46         boolean flag = redis.exists(key);
47         System.out.println(flag);
48         redis.set(key, value);
49         System.out.println("set " + key + " : " + value + ",success");
50     }
51 
52     public static void main(String[] args) {
53         JedisTest2 t1 = new JedisTest2();
54         // String
55         t1.addString("id", "120120");
56 
57         String value = t1.queryString("id");
58         System.out.println("get id >>>" + value);
59 
60         // HashMap
61         HashMap map = new HashMap();
62         map.put("MapId1", "MapValue1");
63         map.put("MapId2", "MapValue2");
64         map.put("MapId3", "MapValue3");
65         t1.addHashMap("hashmap", map);
66 
67         HashSet list = t1.queryHashMapByKey("hashmap");
68         System.out.println(String.format("keys: %s", list));
69     }
70 }

以下备用一个连接池的配置文件

 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
 7     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
 9     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
10     <!-- 连接池配置 -->
11     <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
12         <!-- 最大连接数 -->
13         <property name="maxTotal" value="30" />
14         <!-- 最大空闲连接数 -->
15         <property name="maxIdle" value="10" />
16         <!-- 每次释放连接的最大数目 -->
17         <property name="numTestsPerEvictionRun" value="1024" />
18         <!-- 释放连接的扫描间隔(毫秒) -->
19         <property name="timeBetweenEvictionRunsMillis" value="30000" />
20         <!-- 连接最小空闲时间 -->
21         <property name="minEvictableIdleTimeMillis" value="1800000" />
22         <!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
23         <property name="softMinEvictableIdleTimeMillis" value="10000" />
24         <!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
25         <property name="maxWaitMillis" value="1500" />
26         <!-- 在获取连接的时候检查有效性, 默认false -->
27         <property name="testOnBorrow" value="true" />
28         <!-- 在空闲时检查有效性, 默认false -->
29         <property name="testWhileIdle" value="true" />
30         <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
31         <property name="blockWhenExhausted" value="false" />
32     </bean>  
33     <!-- jedis客户端单机版 -->
34     <bean id="redisClient" class="redis.clients.jedis.JedisPool">
35         <constructor-arg name="host" value="127.0.0.1"></constructor-arg>
36         <constructor-arg name="port" value="6379"></constructor-arg>
37         <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
38     </bean>
39     <bean id="jedisClient" class="com.llh.dao.JedisClientDaoImpl"/>
40      
41     <!-- jedis集群版配置 -->
42     <!-- <bean id="redisClient" class="redis.clients.jedis.JedisCluster">
43         <constructor-arg name="nodes">
44             <set>
45                 <bean class="redis.clients.jedis.HostAndPort">
46                     <constructor-arg name="host" value="192.168.25.153"></constructor-arg>
47                     <constructor-arg name="port" value="7001"></constructor-arg>
48                 </bean>
49                 <bean class="redis.clients.jedis.HostAndPort">
50                     <constructor-arg name="host" value="192.168.25.153"></constructor-arg>
51                     <constructor-arg name="port" value="7002"></constructor-arg>
52                 </bean>
53                 <bean class="redis.clients.jedis.HostAndPort">
54                     <constructor-arg name="host" value="192.168.25.153"></constructor-arg>
55                     <constructor-arg name="port" value="7003"></constructor-arg>
56                 </bean>
57                 <bean class="redis.clients.jedis.HostAndPort">
58                     <constructor-arg name="host" value="192.168.25.153"></constructor-arg>
59                     <constructor-arg name="port" value="7004"></constructor-arg>
60                 </bean>
61                 <bean class="redis.clients.jedis.HostAndPort">
62                     <constructor-arg name="host" value="192.168.25.153"></constructor-arg>
63                     <constructor-arg name="port" value="7005"></constructor-arg>
64                 </bean>
65                 <bean class="redis.clients.jedis.HostAndPort">
66                     <constructor-arg name="host" value="192.168.25.153"></constructor-arg>
67                     <constructor-arg name="port" value="7006"></constructor-arg>
68                 </bean>
69             </set>
70         </constructor-arg>
71         <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
72     </bean>
73     <bean id="jedisClientCluster" class="com.taotao.rest.dao.impl.JedisClientCluster"></bean> -->
74 </beans>

 

项目中使用Redis缓存见后期博客。

 

posted @ 2018-04-10 19:56  念念不忘、  阅读(455)  评论(1编辑  收藏  举报