-
redis安装
下载地址:https://redis.io/download
官网安装:
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
tar xzf redis-5.0.5.tar.gz
cd redis-5.0.5
make
一步安装:
cd /usr/local &&
mkdir redis &&
cd redis &&
wget http://download.redis.io/releases/redis-5.0.5.tar.gz &&
tar xzf redis-5.0.5.tar.gz &&
cd redis-5.0.5 &&
make
-
修改redis.config
#运行外网访问
bind 127.0.0.1 改为 #bind 127.0.0.1
protected-mode yes 改为 protected-mode no
# 后台启动
daemonize no 改为 daemonize yes
-
指定配置文件的方式启动服务
src/redis-server redis.conf
-
新建窗口测试服务是否运行成功
src/redis-cli
set foo bar
get foo
-
redis桌面工具:RedisDesktopManager
下载地址:https://redisdesktop.com/pricing
-
添加pom依赖
<!-- 引入redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
-
修改application.propertis
#=========redis基础配置=========
spring.redis.database=0
spring.redis.host=192.168.43.242
spring.redis.port=6379
# 连接超时时间 单位 ms(毫秒)
spring.redis.timeout=3000#=========redis线程池设置=========
# 连接池中的最大空闲连接,默认值也是8。
spring.redis.pool.max-idle=200#连接池中的最小空闲连接,默认值也是0。
spring.redis.pool.min-idle=200# 如果赋值为-1,则表示不限制;pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
spring.redis.pool.max-active=2000# 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时
spring.redis.pool.max-wait=1000
-
redis简化操作工具类
package com.example.demo.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;@Component
public class RedisClient {@Autowired
private StringRedisTemplate redisTpl;
public boolean set(String key, String value) {
try {
redisTpl.opsForValue().set(key, value);
return true;
} catch (Exception e) {
return false;
}
}
public String get(String key) {
try {
return redisTpl.opsForValue().get(key);
} catch (Exception e) {
return null;
}
}
}
-
json工具类,用于对象和字符串互转
package com.example.demo.utils;
import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
private static ObjectMapper objectMapper = new ObjectMapper();
/**
* 功能描述: string转obj
* @param str
* @param t
* @return
*/
@SuppressWarnings("unchecked")
public static <T> T String2Obj(String str, Class<T> clazz){
if(StringUtils.isEmpty(str) || clazz==null){
return null;
}
try {
return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str, clazz);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 功能描述: object转string
* @param obj
* @return
*/
public static String Obj2String(Object obj){
try{
return obj instanceof String ? (String) obj : objectMapper.writeValueAsString(obj);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
-
测试类
package com.example.demo;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;import com.example.demo.utils.RedisClient;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisTest {@Autowired
private StringRedisTemplate redisTpl;
@Autowired
private RedisClient redis;
@Test
public void testSet1(){
redisTpl.opsForValue().set("test:a1", "aaa");
}
@Test
public void testGet1(){
String result = redisTpl.opsForValue().get("test:a1");
Assert.assertEquals("aaa", result);
}
@Test
public void testSet2(){
redis.set("test:a2", "aaaa");
}
@Test
public void tetGet2(){
String result = redis.get("test:a2");
Assert.assertEquals("aaaa", result);
}
}
-
controller
package com.example.demo.controller;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import com.example.demo.domain.JsonData;
import com.example.demo.entity.User;
import com.example.demo.utils.JsonUtils;
import com.example.demo.utils.RedisClient;@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate redisTpl;
@Autowired
private RedisClient redis;
@RequestMapping("set")
public Object set(@RequestParam String msg){
redisTpl.opsForValue().set("msg", msg);
return JsonData.buildSuccess("ok");
}
@RequestMapping("get")
public Object get(){
String msg = redisTpl.opsForValue().get("msg");
return JsonData.buildSuccess(msg);
}
@RequestMapping("saveuser")
public Object saveUser() {
User user = new User(101, "张三", "", 0, new Date());
redis.set("demo:user:101", JsonUtils.Obj2String(user));
return JsonData.buildSuccess(user);
}
@RequestMapping("getuser")
public Object getUser(){
String data = redis.get("demo:user:101");
User user = JsonUtils.String2Obj(data, User.class);
return JsonData.buildSuccess(user);
}
}