redis

Posted cdwxs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis相关的知识,希望对你有一定的参考价值。

springboot整合redis:

第一:添加相关jar包

 <dependency>

  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

第二:在application.properties配置redis链接信息

spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=5000

第三:封装redis操作持久类

/**
* map 和list存放对象,首先把list和map转为json后,通过sting类型来存放
*
* @author Administrator
*
*/
@Service
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;

public void setString(String key, Object value) {
setObject(key, value, null);
}

public void setString(String key, Object value, Long time) {
setObject(key, value, time);
}

public void setList(String key, List<String> list) {
setObject(key, list, null);
}

public void setList(String key, List<String> valueList, Long time) {
setObject(key, valueList, time);
}

public void setMap(String key, Map<String, Object> map) {
setObject(key, map, null);
}
public void setMap(String key, Map<String, Object> map, Long time) {
setObject(key, map, time);
}

// time,设置数据在redis保存时间
public void setObject(String key, Object value, Long time) {
if (StringUtils.isEmpty(key) || value == null) {
return;
}

if (value instanceof String) {
if (time != null) {
stringRedisTemplate.opsForValue().set(key, (String) value, time, TimeUnit.SECONDS);
} else {
stringRedisTemplate.opsForValue().set(key, (String) value);
}
return;
}

if (value instanceof List) {
// 存放list類型
/*
* List<String> listValue = (List<String>) value; for (String string :
* listValue) { stringRedisTemplate.opsForList().leftPush(key, string); }
*/
List obj = (List) value;
stringRedisTemplate.opsForList().leftPushAll(key, obj);
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
}

if (value instanceof Map) {
HashOperations<String, String, Object> opsForHash = stringRedisTemplate.opsForHash();
opsForHash.putAll(key, (Map) value);
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);// 设置超时时间10秒 第三个参数控制时间单位,
}

}
// 封装map,set,排序set

public boolean existKey(String key) {
return stringRedisTemplate.hasKey(key);
}

public void delKey(String key) {
stringRedisTemplate.delete(key);
}

public String getString(String key) {
boolean flage = existKey(key);
if (flage) {
return stringRedisTemplate.opsForValue().get(key);
} else {
return null;
}
}

public List<String> getListValue(String key) {
boolean flage = existKey(key);
if (flage) {
return stringRedisTemplate.opsForList().range(key, 0, -1);// -1,表示所有的元素
} else {
return null;
}
}

public Map<String, Object> getMapValue(String key) {
boolean flage = existKey(key);
if (flage) {
HashOperations<String, String, Object> hps = stringRedisTemplate.opsForHash();
return hps.entries(key);
} else {
return null;
}

}
}

第四:封装Json类,用于Object和Json直接转换

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;

import com.alibaba.fastjson.JSON;

public class JSONSerializer {
private static final String DEFAULT_CHARSET_NAME = "UTF-8";

//对象转为json
public static <T> String toJSONString(T object) {
return JSON.toJSONString(object);
}

//json转为对象
public static <T> T parseObject(String string, Class<T> clz) {
return JSON.parseObject(string, clz);
}

//jsonArray转为list(List转为json后再转为list的方法)
public static List JSONArrayToList(String jsonArray,Class clz){
return JSON.parseArray(jsonArray,clz);
}

public static <T> T load(Path path, Class<T> clz) throws IOException {
return parseObject(new String(Files.readAllBytes(path), DEFAULT_CHARSET_NAME), clz);
}

public static <T> void save(Path path, T object) throws IOException {
if (Files.notExists(path.getParent())) {
Files.createDirectories(path.getParent());
}
Files.write(path, toJSONString(object).getBytes(DEFAULT_CHARSET_NAME), StandardOpenOption.WRITE,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
}
}

第五:controller测试类

 

以上是关于redis的主要内容,如果未能解决你的问题,请参考以下文章

RedisRedis 安装启动使用流程

RedisRedis 安装启动使用流程

redisredis知识点总结

redisredis知识点总结

RedisRedis高级

RedisRedis简述