From fbb4484c372f891f94432d3503003b227e138192 Mon Sep 17 00:00:00 2001 From: meng <154040976@qq.com> Date: Sat, 24 Nov 2018 11:30:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E4=B8=8Bredis=E7=BC=93?= =?UTF-8?q?=E5=AD=98=20=20=20=20=20=20=20=20=20=20=20--=E5=90=AC=E4=B8=80?= =?UTF-8?q?=E9=A6=96=E6=AD=8C=EF=BC=8C=E5=9B=9E=E5=BF=86=E4=B8=80=E6=AE=B5?= =?UTF-8?q?=E5=BE=80=E4=BA=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/len/redis/RedisService.java | 99 +++++++++++++++++++ len-web/src/main/java/test/BootTest.java | 30 ++++-- .../main/resources/application-mysql-dev.yml | 8 +- .../src/main/resources/application-prod.yml | 8 +- pom.xml | 5 + 5 files changed, 140 insertions(+), 10 deletions(-) create mode 100644 len-core/src/main/java/com/len/redis/RedisService.java diff --git a/len-core/src/main/java/com/len/redis/RedisService.java b/len-core/src/main/java/com/len/redis/RedisService.java new file mode 100644 index 0000000..d27c3ec --- /dev/null +++ b/len-core/src/main/java/com/len/redis/RedisService.java @@ -0,0 +1,99 @@ +package com.len.redis; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.concurrent.TimeUnit; + +/** + * @author zhuxiaomeng + * @date 2018/11/24. + * @email 154040976@qq.com + */ +@Service +public class RedisService { + + @Autowired + private StringRedisTemplate stringRedisTemplate; + + @Autowired + private RedisTemplate redisTemplate; + + @Resource(name = "stringRedisTemplate") + private ValueOperations valueOps; + + @Resource(name = "redisTemplate") + private ValueOperations valOpsObj; + + @Value("${redis.prefix}") + private String prefix; + + /** + * 获取缓存字符串 根据key + * + * @param key 字符串key + * @return value + */ + public String get(String key) { + key = prefix + key; + return valueOps.get(key); + } + + /** + * set value and cache timeout by str key + * + * @param key 字符串key + * @param value 字符串value + * @param second 过期时间 单位 秒 + */ + public void set(String key, String value, Long second) { + key = prefix + key; + valueOps.set(key, value, second,TimeUnit.SECONDS); + } + + /** + * get object value by key + * + * @param key obj key + * @return obj value + */ + public Object getObj(Object key) { + key = prefix + key.toString(); + return valOpsObj.get(key); + } + + /** + * set value by key + * + * @param key obj key + * @param value obj value + * @param second 过期时间 单位户 秒 + */ + public void setObj(Object key, Object value, Long second) { + key = prefix + key.toString(); + valOpsObj.set(key, value, second,TimeUnit.SECONDS); + } + + /** + * delete by key + * + * @param key key + */ + public void del(String key) { + stringRedisTemplate.delete(prefix + key); + } + + /** + * delete by key + * + * @param key obj key + */ + public void delObj(Object key) { + redisTemplate.delete(prefix + key.toString()); + } +} diff --git a/len-web/src/main/java/test/BootTest.java b/len-web/src/main/java/test/BootTest.java index 0b563bd..9f178f3 100644 --- a/len-web/src/main/java/test/BootTest.java +++ b/len-web/src/main/java/test/BootTest.java @@ -2,6 +2,7 @@ package test; import com.len.Application; import com.len.entity.SysUser; +import com.len.redis.RedisService; import com.len.service.SysUserService; import org.junit.Test; import org.junit.runner.RunWith; @@ -20,14 +21,27 @@ import org.springframework.test.context.web.WebAppConfiguration; @WebAppConfiguration public class BootTest { - @Autowired - SysUserService userService; + @Autowired + SysUserService userService; - @Test - public void testStartJob() throws Exception { - //12 - SysUser user=userService.selectByPrimaryKey("2211fec3e17c11e795ed201a068c6482"); - System.out.println(user.getUsername()); - } + @Autowired + RedisService redisService; + + @Test + public void testStartJob() throws Exception { + //12 + SysUser user = userService.selectByPrimaryKey("2211fec3e17c11e795ed201a068c6482"); + System.out.println(user.getUsername()); + } + + @Test + public void redisTest() throws InterruptedException { + redisService.set("str", "你好 世界", 10L); + String str = redisService.get("str"); + System.out.println("value:" + str); + Thread.sleep(1000 * 10L); + System.out.println("value:" + redisService.get("str")); + + } } \ No newline at end of file diff --git a/len-web/src/main/resources/application-mysql-dev.yml b/len-web/src/main/resources/application-mysql-dev.yml index 99570da..eaf1ba4 100644 --- a/len-web/src/main/resources/application-mysql-dev.yml +++ b/len-web/src/main/resources/application-mysql-dev.yml @@ -32,6 +32,9 @@ spring: client: prefer-ip: true url: http://localhost:8082 + redis: + host: 127.0.0.1 + port: 6379 mybatis: type-aliases-package: com.len.entity @@ -64,4 +67,7 @@ logging: level: com.len.mapper: debug -dataType: mysql \ No newline at end of file +dataType: mysql + +redis: + prefix: len_ \ No newline at end of file diff --git a/len-web/src/main/resources/application-prod.yml b/len-web/src/main/resources/application-prod.yml index abf53e3..06078f6 100644 --- a/len-web/src/main/resources/application-prod.yml +++ b/len-web/src/main/resources/application-prod.yml @@ -31,6 +31,9 @@ spring: client: prefer-ip: true url: http://localhost:8082 + redis: + host: 127.0.0.1 + port: 6379 mybatis: type-aliases-package: com.len.entity @@ -57,4 +60,7 @@ logging: filePath: ./file/ -imagePath: ./image/ \ No newline at end of file +imagePath: ./image/ + +redis: + prefix: len_ \ No newline at end of file diff --git a/pom.xml b/pom.xml index 462fdc8..6f3cc6d 100644 --- a/pom.xml +++ b/pom.xml @@ -93,6 +93,11 @@ org.springframework.boot spring-boot-starter-logging + + org.springframework.boot + spring-boot-starter-data-redis + 2.0.1.RELEASE +