支持下redis缓存

--听一首歌,回忆一段往事
This commit is contained in:
meng 2018-11-24 11:30:36 +08:00
parent 9715b3a916
commit fbb4484c37
5 changed files with 140 additions and 10 deletions

View File

@ -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<Object, Object> redisTemplate;
@Resource(name = "stringRedisTemplate")
private ValueOperations<String, String> valueOps;
@Resource(name = "redisTemplate")
private ValueOperations<Object, Object> 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());
}
}

View File

@ -2,6 +2,7 @@ package test;
import com.len.Application; import com.len.Application;
import com.len.entity.SysUser; import com.len.entity.SysUser;
import com.len.redis.RedisService;
import com.len.service.SysUserService; import com.len.service.SysUserService;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -20,14 +21,27 @@ import org.springframework.test.context.web.WebAppConfiguration;
@WebAppConfiguration @WebAppConfiguration
public class BootTest { public class BootTest {
@Autowired @Autowired
SysUserService userService; SysUserService userService;
@Test @Autowired
public void testStartJob() throws Exception { RedisService redisService;
//12
SysUser user=userService.selectByPrimaryKey("2211fec3e17c11e795ed201a068c6482"); @Test
System.out.println(user.getUsername()); 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"));
}
} }

View File

@ -32,6 +32,9 @@ spring:
client: client:
prefer-ip: true prefer-ip: true
url: http://localhost:8082 url: http://localhost:8082
redis:
host: 127.0.0.1
port: 6379
mybatis: mybatis:
type-aliases-package: com.len.entity type-aliases-package: com.len.entity
@ -65,3 +68,6 @@ logging:
com.len.mapper: debug com.len.mapper: debug
dataType: mysql dataType: mysql
redis:
prefix: len_

View File

@ -31,6 +31,9 @@ spring:
client: client:
prefer-ip: true prefer-ip: true
url: http://localhost:8082 url: http://localhost:8082
redis:
host: 127.0.0.1
port: 6379
mybatis: mybatis:
type-aliases-package: com.len.entity type-aliases-package: com.len.entity
@ -58,3 +61,6 @@ logging:
filePath: ./file/ filePath: ./file/
imagePath: ./image/ imagePath: ./image/
redis:
prefix: len_

View File

@ -93,6 +93,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId> <artifactId>spring-boot-starter-logging</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!-- <dependency> <!-- <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>