├── README.md
├── spring-redis
├── pom.xml
└── src
│ ├── main
│ ├── java
│ │ └── com
│ │ │ └── spring
│ │ │ └── redis
│ │ │ ├── controller
│ │ │ └── RedisController.java
│ │ │ ├── pojo
│ │ │ └── User.java
│ │ │ └── util
│ │ │ └── RedisUtil.java
│ ├── resources
│ │ ├── log4j.properties
│ │ ├── redis.properties
│ │ └── spring
│ │ │ ├── applicationContext-redis.xml
│ │ │ ├── applicationContext.xml
│ │ │ └── spring-redis-servlet.xml
│ └── webapp
│ │ └── WEB-INF
│ │ └── web.xml
│ └── test
│ └── java
│ └── com
│ └── spring
│ └── redis
│ └── util
│ └── RedisUtilTest.java
├── wyait-common
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── wyait
│ └── common
│ └── utils
│ ├── DateUtil.java
│ ├── JSONUtils.java
│ └── ValidateUtil.java
└── wyait-redis
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── wyait
│ │ └── redis
│ │ ├── WyaitRedisApplication.java
│ │ ├── config
│ │ └── RedisConfig.java
│ │ ├── controller
│ │ └── RedisCacheController.java
│ │ ├── pojo
│ │ └── Cat.java
│ │ ├── service
│ │ └── CatService.java
│ │ └── utils
│ │ └── RedisUtils.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── wyait
└── redis
└── WyaitRedisApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # project
2 | project 公开项目,供大家使用,快速上手;并附上博客链接,详细描述使用方法和注意事项; 项目中的技术框架稳定版本不定时更新,欢迎关注;
3 |
4 | 项目中的技术框架稳定版本不定时更新,欢迎关注;
5 |
6 | **项目说明**:
7 | [ **wyait-common** ](https://github.com/wyait/project.git) : 通用工具类;
8 | [ **wyait-redis** ](https://github.com/wyait/project.git):[spring boot 1.5.9整合redis;包含redis封装的API工具类和缓存管理的注解配置和使用;http://blog.51cto.com/wyait/2048478](http://blog.51cto.com/wyait/2048478);
9 | [ **spring-redis** ](https://github.com/wyait/project.git):[spring整合redis;包含redis封装的API工具类和缓存管理的配置、使用和注意事项;http://blog.51cto.com/wyait/2049866](http://blog.51cto.com/wyait/2049866);
10 | [ **wyait-manage** ](https://github.com/wyait/project.git):[spring boot + mybatis + shiro + layui 搭建的后台权限管理系统;http://blog.51cto.com/wyait/2082803](http://blog.51cto.com/wyait/2082803);
11 |
--------------------------------------------------------------------------------
/spring-redis/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.spring.redis
5 | spring-redis
6 | 0.0.1-SNAPSHOT
7 | war
8 |
9 |
21 |
22 | org.springframework
23 | spring-context
24 | 4.3.5.RELEASE
25 |
26 |
27 | org.springframework
28 | spring-beans
29 | 4.3.5.RELEASE
30 |
31 |
32 | org.springframework
33 | spring-webmvc
34 | 4.3.5.RELEASE
35 |
36 |
37 |
38 | redis.clients
39 | jedis
40 | 2.9.0
41 |
42 |
43 |
44 | org.springframework.data
45 | spring-data-redis
46 | 1.7.5.RELEASE
47 |
48 |
49 |
50 | com.fasterxml.jackson.core
51 | jackson-databind
52 | 2.8.9
53 |
54 |
55 |
56 | org.apache.commons
57 | commons-lang3
58 | 3.6
59 |
60 |
61 | commons-io
62 | commons-io
63 | 2.5
64 |
65 |
66 |
67 | org.slf4j
68 | slf4j-log4j12
69 | 1.7.25
70 |
71 |
72 |
73 |
74 |
75 |
76 | org.apache.tomcat.maven
77 | tomcat7-maven-plugin
78 |
79 | 8063
80 | /
81 |
82 |
83 |
84 |
85 |
--------------------------------------------------------------------------------
/spring-redis/src/main/java/com/spring/redis/controller/RedisController.java:
--------------------------------------------------------------------------------
1 | package com.spring.redis.controller;
2 |
3 | import java.util.HashMap;
4 |
5 | import org.springframework.beans.factory.annotation.Autowired;
6 | import org.springframework.stereotype.Controller;
7 | import org.springframework.web.bind.annotation.RequestMapping;
8 | import org.springframework.web.bind.annotation.RequestMethod;
9 | import org.springframework.web.bind.annotation.ResponseBody;
10 |
11 | import com.spring.redis.pojo.User;
12 | import com.spring.redis.util.RedisUtil;
13 |
14 | @Controller
15 | @RequestMapping("/")
16 | public class RedisController {
17 | @Autowired
18 | private RedisUtil redisUtil;
19 |
20 | @RequestMapping(value = "add", method = RequestMethod.GET)
21 | @ResponseBody
22 | public String add() {
23 | System.out.println("================华丽进=======================");
24 | this.redisUtil.set("aaa", "aaa");
25 | System.out.println("================华丽出=======================");
26 | return "hello world";
27 | }
28 |
29 | @RequestMapping(value = "get", method = RequestMethod.GET)
30 | @ResponseBody
31 | public String get() {
32 | System.out.println("================获取redis中数据=======================");
33 | return String.valueOf(this.redisUtil.get("aaa"));
34 | }
35 |
36 | @RequestMapping(value = "addHash", method = RequestMethod.GET)
37 | @ResponseBody
38 | public String addHash() {
39 | System.out.println("================华丽进===addHash====================");
40 | User u = new User();
41 | u.setAge(11);
42 | u.setId(1L);
43 | u.setName("李四");
44 | // 存储hash结构的数据,可以是HashMap
45 | this.redisUtil.setHash("mmm", "bbb:ccc", u);
46 | HashMap map = new HashMap();
47 | map.put("user", u);
48 | this.redisUtil.setHash("mmm", "nnn", map);
49 | System.out.println("================华丽出====addHash===================");
50 | return "hello world";
51 | }
52 |
53 | @RequestMapping(value = "getHash", method = RequestMethod.GET)
54 | @ResponseBody
55 | public HashMap getHash() {
56 | System.out
57 | .println("================获取redis中数据========getHash===============");
58 | User user = (User) this.redisUtil.getHash("mmm", "bbb:ccc");
59 | System.out
60 | .println("================获取redis中数据========getHash==============="
61 | + user);
62 | return (HashMap) this.redisUtil.getHash("mmm", "nnn");
63 | }
64 |
65 | }
66 |
--------------------------------------------------------------------------------
/spring-redis/src/main/java/com/spring/redis/pojo/User.java:
--------------------------------------------------------------------------------
1 | package com.spring.redis.pojo;
2 |
3 | import java.io.Serializable;
4 |
5 | public class User implements Serializable {
6 | private static final long serialVersionUID = 1L;
7 | private Long id;
8 | private Integer age;
9 | private String name;
10 |
11 | public Long getId() {
12 | return id;
13 | }
14 |
15 | public void setId(Long id) {
16 | this.id = id;
17 | }
18 |
19 | public Integer getAge() {
20 | return age;
21 | }
22 |
23 | public void setAge(Integer age) {
24 | this.age = age;
25 | }
26 |
27 | public String getName() {
28 | return name;
29 | }
30 |
31 | public void setName(String name) {
32 | this.name = name;
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "User [id=" + id + ", age=" + age + ", name=" + name + "]";
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/spring-redis/src/main/java/com/spring/redis/util/RedisUtil.java:
--------------------------------------------------------------------------------
1 | package com.spring.redis.util;
2 |
3 | import java.util.List;
4 | import java.util.Map;
5 | import java.util.Set;
6 | import java.util.concurrent.TimeUnit;
7 |
8 | import org.apache.commons.lang3.StringUtils;
9 | import org.springframework.data.redis.core.RedisTemplate;
10 | import org.springframework.util.CollectionUtils;
11 |
12 | /**
13 | *
14 | * @项目名称:common
15 | * @类名称:RedisUtil
16 | * @类描述:基于spring和redis的redisTemplate工具类;
17 | * @创建人:wyait
18 | * @创建时间:2017年12月8日 下午3:32:38
19 | * @version:V1.0
20 | */
21 | // @Component
22 | public class RedisUtil {
23 | // 通过构造方法注入
24 | // @Autowired
25 | private RedisTemplate redisTemplate;
26 |
27 | /*
28 | * 如果使用注解注入RedisTemplate对象,则不需要该setter方法
29 | */
30 | public void setRedisTemplate(RedisTemplate redisTemplate) {
31 | this.redisTemplate = redisTemplate;
32 | }
33 |
34 | /**
35 | * String类型缓存获取
36 | * @param key 键
37 | * @return 值
38 | */
39 | public Object get(String key) {
40 | return key == null ? null : redisTemplate.opsForValue().get(key);
41 | }
42 |
43 | /**
44 | * String类型缓存保存
45 | * @param key 键
46 | * @param value 值
47 | * @return true:成功;false:失败
48 | */
49 | public boolean set(String key, Object value) {
50 | try {
51 | if (StringUtils.isNotEmpty(key) && null != value) {
52 | redisTemplate.opsForValue().set(key, value);
53 | return true;
54 | }
55 | } catch (Exception e) {
56 | e.printStackTrace();
57 | }
58 | return false;
59 | }
60 |
61 | /**
62 | * 指定缓存失效时间
63 | * @param key 键
64 | * @param time 时间(秒)
65 | * @return true:成功;false:失败
66 | */
67 | public boolean expire(String key, long time) {
68 | try {
69 | if (StringUtils.isNotEmpty(key) && time > 0) {
70 | redisTemplate.expire(key, time, TimeUnit.SECONDS);
71 | return true;
72 | }
73 | } catch (Exception e) {
74 | e.printStackTrace();
75 | }
76 | return false;
77 | }
78 |
79 | /**
80 | * 根据key获取过期时间
81 | * @param key 键 不能为null
82 | * @return 时间(秒)返回0代表为永久有效;-1代表key不存在
83 | */
84 | public long getExpire(String key) {
85 | if (StringUtils.isNotEmpty(key)) {
86 | return redisTemplate.getExpire(key, TimeUnit.SECONDS);
87 | }
88 | return -1L;
89 | }
90 |
91 | /**
92 | * 判断key是否存在
93 | * @param key 键
94 | * @return true:存在;false:不存在
95 | */
96 | public boolean exists(String key) {
97 | try {
98 | if (StringUtils.isNotEmpty(key)) {
99 | return redisTemplate.hasKey(key);
100 | }
101 | } catch (Exception e) {
102 | e.printStackTrace();
103 | }
104 | return false;
105 | }
106 |
107 | /**
108 | * 删除缓存(批量)
109 | * @param keys(可变参数)可以传一个值或多个
110 | */
111 | @SuppressWarnings("unchecked")
112 | public void delete(String... keys) {
113 | if (keys != null && keys.length > 0) {
114 | if (keys.length == 1) {
115 | redisTemplate.delete(keys[0]);
116 | } else {
117 | // 批量删除
118 | redisTemplate.delete(CollectionUtils.arrayToList(keys));
119 | }
120 | }
121 | }
122 |
123 | /**
124 | * String类型缓存保存并设置时间
125 | * @param key 键
126 | * @param value 值
127 | * @param time 时间(秒):time要大于0 如果time小于等于0 将设置无限期
128 | * @return true:成功;false:失败
129 | */
130 | public boolean set(String key, Object value, long time) {
131 | try {
132 | if (StringUtils.isNotEmpty(key) && null != value) {
133 | if (time > 0) {
134 | redisTemplate.opsForValue().set(key, value, time,
135 | TimeUnit.SECONDS);
136 | } else {
137 | set(key, value);
138 | }
139 | return true;
140 | }
141 | } catch (Exception e) {
142 | e.printStackTrace();
143 | }
144 | return false;
145 | }
146 |
147 | /**
148 | * 递增
149 | * @param key 键
150 | * @param by 要增加几(大于0)
151 | * @return
152 | */
153 | public long increment(String key, long delta) {
154 | if (delta < 0) {
155 | throw new RuntimeException("递增因子必须大于0");
156 | }
157 | if (StringUtils.isNotEmpty(key)) {
158 | return redisTemplate.opsForValue().increment(key, delta);
159 | } else {
160 | return -1L;
161 | }
162 | }
163 |
164 | /**
165 | * 递减
166 | * @param key 键
167 | * @param by 要减少几(小于0)
168 | * @return
169 | */
170 | public long decrement(String key, long delta) {
171 | if (delta < 0) {
172 | throw new RuntimeException("递减因子必须大于0");
173 | }
174 | if (StringUtils.isNotEmpty(key)) {
175 | return redisTemplate.opsForValue().increment(key, -delta);
176 | } else {
177 | return -1L;
178 | }
179 | }
180 |
181 | /**
182 | * 获取hash结构的数据 (Hash类型)
183 | * @param key 键: 不能为null
184 | * @param item 项: 不能为null
185 | * @return 值
186 | */
187 | public Object getHash(String key, String item) {
188 | if (StringUtils.isNotEmpty(key) && StringUtils.isNotEmpty(item)) {
189 | return redisTemplate.opsForHash().get(key, item);
190 | } else {
191 | return null;
192 | }
193 | }
194 |
195 | /**
196 | * 获取Key对应的所有键值HashMap
197 | * @param key 键
198 | * @return 对应的多个键值
199 | */
200 | public Map