码迷,mamicode.com
首页 > 其他好文 > 详细

redis(二)

时间:2019-01-30 00:23:32      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:ica   array   lib   open   list()   creat   hal   ash   sse   

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(二)

标签:ica   array   lib   open   list()   creat   hal   ash   sse   

原文地址:https://www.cnblogs.com/xianshiwang/p/10336296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!