From 6b92675a6778571d958c61fa8d15e2ecf5d2e425 Mon Sep 17 00:00:00 2001
From: wenjie <1569925@qq.com>
Date: Fri, 15 Dec 2017 16:38:19 +0800
Subject: [PATCH 01/22] WellJay
new version
---
.gitignore | 2 +
README.md | 124 +++++++--
impl/RedisDaoTool.java | 386 ---------------------------
inter/AbstractBaseRedisDao.java | 27 --
redisTools/RedisCacheUtil.java | 191 +++++++++++++
testController/SystemController.java | 34 ---
6 files changed, 289 insertions(+), 475 deletions(-)
delete mode 100644 impl/RedisDaoTool.java
delete mode 100644 inter/AbstractBaseRedisDao.java
create mode 100644 redisTools/RedisCacheUtil.java
delete mode 100644 testController/SystemController.java
diff --git a/.gitignore b/.gitignore
index 32858aa..cfdbd93 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,5 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
+*.xml
+.idea/spring-data-redis-tools.iml
diff --git a/README.md b/README.md
index b78706b..23278fd 100644
--- a/README.md
+++ b/README.md
@@ -1,34 +1,102 @@
# spring-data-redis-tools
-spring data redis 封装工具类
+spring data redis 封装工具类
+近期会持续增加分布式锁等功能
***
## Maven
-```html
-
- redis.clients
- jedis
- 2.6.2
-
-
- org.springframework.data
- spring-data-redis
- 1.5.1.RELEASE
-
+```xml
+
+ org.springframework.boot
+ spring-boot-starter-data-redis
+
```
-## applicationContext.xml
-```html
-
-
-
-
-
-
-
+## RedisConfiguration
+```java
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.annotation.EnableCaching;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.data.redis.cache.RedisCacheManager;
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
+import org.springframework.data.redis.core.RedisTemplate;
+import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
+import org.springframework.data.redis.serializer.StringRedisSerializer;
+import redis.clients.jedis.JedisPoolConfig;
-
-
-
+/**
+ * @author wellJay
+ */
+@Configuration
+@EnableCaching
+public class RedisConfiguration {
+ //过期时间一天
+ private static final int DEFAULT_EXPIRE_TIME = 3600 * 24;
+
+ //从配置文件读取redis参数
+ @Autowired
+ private CloudConfigProperties cloudConfigProperties;
+
+ /**
+ * jedisPoolConfig config
+ */
+ @Bean
+ public JedisPoolConfig jedisPoolConfig() {
+ JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
+ jedisPoolConfig.setMaxIdle(cloudConfigProperties.getRedis().getMaxIdle());
+ jedisPoolConfig.setMinIdle(cloudConfigProperties.getRedis().getMinIdle());
+ jedisPoolConfig.setTestOnBorrow(cloudConfigProperties.getRedis().getTestOnBorrow());
+ jedisPoolConfig.setTestOnReturn(cloudConfigProperties.getRedis().getTestOnReturn());
+ return jedisPoolConfig;
+ }
+
+ /**
+ * JedisConnectionFactory
+ */
+ @Bean
+ public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
+ JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
+ jedisConnectionFactory.setHostName(cloudConfigProperties.getRedis().getHost());
+ jedisConnectionFactory.setPort(cloudConfigProperties.getRedis().getPort());
+ jedisConnectionFactory.setPassword(cloudConfigProperties.getRedis().getPassword());
+ jedisConnectionFactory.setTimeout(cloudConfigProperties.getRedis().getTimeout());
+ jedisConnectionFactory.setUsePool(true);
+ jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
+ return jedisConnectionFactory;
+ }
+
+ /**
+ * RedisTemplate
+ * 从执行时间上来看,JdkSerializationRedisSerializer是最高效的(毕竟是JDK原生的),但是是序列化的结果字符串是最长的。
+ * JSON由于其数据格式的紧凑性,序列化的长度是最小的,时间比前者要多一些。
+ * 所以个人的选择是倾向使用JacksonJsonRedisSerializer作为POJO的序列器。
+ */
+ @Bean
+ public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
+ RedisTemplate, ?> redisTemplate = new RedisTemplate();
+ redisTemplate.setConnectionFactory(jedisConnectionFactory);
+ redisTemplate.setDefaultSerializer(new StringRedisSerializer());
+ //设置普通value序列化方式
+ redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
+ redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
+ return redisTemplate;
+ }
+
+ @Bean
+ public CacheManager cacheManager(RedisTemplate redisTemplate) {
+ RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
+ redisCacheManager.setDefaultExpiration(DEFAULT_EXPIRE_TIME);
+ return redisCacheManager;
+ }
+}
+```
+
+## How To Use
+1、注入util方式,适用于复杂的业务处理
+```java
+ @Autowired
+ private RedisCacheUtil redisCacheUtil;
+```
+2、Spring注解方式适用于简单的数据缓存
+```java
+ @Cacheable(value = Constants.Redis.SYSTEM, key = ACTIONS_CACHE_KEY)
```
diff --git a/impl/RedisDaoTool.java b/impl/RedisDaoTool.java
deleted file mode 100644
index a7c7fea..0000000
--- a/impl/RedisDaoTool.java
+++ /dev/null
@@ -1,386 +0,0 @@
-package com.brandbigdata.rep.redis.impl;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import org.springframework.dao.DataAccessException;
-import org.springframework.data.redis.connection.RedisConnection;
-import org.springframework.data.redis.core.RedisCallback;
-import org.springframework.stereotype.Repository;
-
-import com.brandbigdata.rep.redis.inter.AbstractBaseRedisDao;
-
-/**
- * @author Wen Jie
- *
- */
-@Repository
-public class StringRedisDao extends AbstractBaseRedisDao {
-
- // ----------------------------------------Object----------------------------------------
- /**
- * 设置对象
- *
- * @param key
- * @param object
- * @param timeout
- * @param 对象class
- * @return
- * @throws Exception
- */
- public boolean addObject(final String key, final Object object, final Long timeout, Class> clazz) throws BbdException {
- redisTemplate.opsForValue().set(key, clazz.cast(object));
- return true;
- }
-
- /**
- * 获得对象
- *
- * @param key
- * @return
- */
- public Object getObject(final String key) {
- return redisTemplate.opsForValue().get(key);
- }
-
- // ---------------------------------------String-----------------------------------------
-
- /**
- * 新增String ----setNX 不存在则增加 ------------------------------
- *
- * @param key
- * 键
- * @param value
- * 值
- * @param timout
- * 超时(秒)
- * @return true 操作成功,false 已存在值
- */
- public boolean addString(final String key, final String value, final Long timeout) {
- boolean result = redisTemplate.execute(new RedisCallback() {
- public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
- Boolean result = connection.setNX(key.getBytes(), value.getBytes());
- if (result == false)
- return result;
- if (timeout != null && timeout > 0)
- connection.expire(key.getBytes(), timeout);
- return result;
- }
- });
- return result;
- }
-
- /**
- * 批量新增String---setNx 不存在则增加
- *
- * @param keyValueList
- * 键值对的map
- * @param timeout
- * 超时处理
- * @return
- */
- public boolean addString(final Map keyValueList, final Long timeout) {
- boolean result = redisTemplate.execute(new RedisCallback() {
- public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
- for (String key : keyValueList.keySet()) {
- connection.setNX(key.getBytes(), keyValueList.get(key).getBytes());
- if (timeout != null && timeout > 0)
- connection.expire(key.getBytes(), timeout);
- }
- return true;
- }
- }, false, true);
- return result;
- }
-
- /**
- * 通过key获取单个
- *
- * @param key
- * @return
- */
- public String getString(final String key) {
- String value = redisTemplate.execute(new RedisCallback() {
- public String doInRedis(RedisConnection connection) throws DataAccessException {
- byte[] result = connection.get(key.getBytes());
- if(result != null && result.length > 0)
- return new String(result);
- return null;
- }
- });
- return value;
- }
-
- /**
- * 修改 String
- *
- * @param key
- * @param value
- * @return
- */
- /*
- 重新Set等于Update
- public boolean updateString(final String key, final String value) {
- if (getString(key) == null) {
- throw new NullPointerException("数据行不存在, key = " + key);
- }
- boolean result = redisTemplate.execute(new RedisCallback() {
- public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
- connection.set(key.getBytes(), value.getBytes());
- return true;
- }
- });
- return result;
- }
- /
-
- // ---------------------------------------List-----------------------------------------
- /**
- * 新增Hash ----setNX 不存在则增加 ------------------------------
- *
- * @param key
- * 键
- * @param value
- * 值
- * @param timout
- * 超时(秒)
- * @return true 操作成功,false 已存在值
- */
- public boolean addHash(final String key, final String field, final String value, final Long timeout) {
- boolean result = redisTemplate.execute(new RedisCallback() {
- public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
- Boolean result = connection.hSetNX(key.getBytes(), field.getBytes(), value.getBytes());
- if (result == false)
- return result;
- if (timeout != null && timeout > 0)
- connection.expire(key.getBytes(), timeout);
- return result;
- }
- });
- return result;
- }
-
- /**
- * 批量新增Hash ----setNX 不存在则增加 ------------------------------
- *
- * @param key
- * 键
- * @param value
- * 值
- * @param timout
- * 超时(秒)
- * @return true 操作成功,false 已存在值
- */
- public boolean addHash(final String key, final Map fieldValueList, final Long timeout) {
- boolean result = redisTemplate.execute(new RedisCallback() {
- public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
- for (String hashKey : fieldValueList.keySet()) {
- connection.hSetNX(key.getBytes(), hashKey.getBytes(), fieldValueList.get(hashKey).getBytes());
- if (timeout != null && timeout > 0)
- connection.expire(key.getBytes(), timeout);
- }
- return true;
- }
- });
- return result;
- }
-
- /**
- * 通过key获取单个
- *
- * @param key
- * @return
- */
- public Object getHashField(final String key, final String field) {
- String value = redisTemplate.execute(new RedisCallback() {
- public String doInRedis(RedisConnection connection) throws DataAccessException {
- return new String(connection.hGet(key.getBytes(), field.getBytes()));
- }
- });
- return value;
- }
-
- /**
- * 通过key获取整个Hash
- *
- * @param key
- * @return
- */
- public Map getHashAll(final String key, final String field) {
- Map value = redisTemplate.execute(new RedisCallback