前言
大家都知道在項(xiàng)目中,緩存以及mq消息隊(duì)列可以說(shuō)是不可或缺的2個(gè)重要技術(shù)。前者主要是為了減輕數(shù)據(jù)庫(kù)壓力,大幅度提升性能。后者主要是為了提高用戶的體驗(yàn)度,我理解的是再后端做的一個(gè)ajax請(qǐng)求(異步),并且像ribbmitmq等消息隊(duì)列有重試機(jī)制等功能。
這里主要講redis如何把對(duì)象,集合存入,并且取出。下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
1.在啟動(dòng)類(lèi)上加入如下代碼
private Jedis jedis;private JedisPoolConfig config;private JedisShardInfo sharInfo;@Beanpublic Jedis jedis(){//連接redis服務(wù)器,192.168.0.100:6379// jedis = new Jedis("192.168.0.100", 6379);// //權(quán)限認(rèn)證// jedis.auth("123456");// 操作單獨(dú)的文本串config = new JedisPoolConfig();
config.setMaxIdle(1000);//最大空閑時(shí)間config.setMaxWaitMillis(1000); //最大等待時(shí)間config.setMaxTotal(500); //redis池中最大對(duì)象個(gè)數(shù)sharInfo = new JedisShardInfo("192.168.0.100", 6379);
sharInfo.setPassword("123456");
sharInfo.setConnectionTimeout(5000);//鏈接超時(shí)時(shí)間jedis = new Jedis(sharInfo);return jedis;
}
2.在application.yml當(dāng)中加入redis配置
spring:
redis:
database: 0
host: 101.132.191.77
port: 6379
password: 123456
pool:
max-idle: 8 #連接池最大連接數(shù)(使用負(fù)值表示沒(méi)有限制)
min-idle: 0 # 連接池中的最小空閑連接
max-active: 8 # 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒(méi)有限制)
max-wait: -1 # 連接池中的最大空閑連接
timeout: 5000 # 連接超時(shí)時(shí)間(毫秒)
3.新建SerializeUtil類(lèi),這個(gè)類(lèi)主要是為了將對(duì)象序列化redis當(dāng)中
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;/**
public class SerializeUtil
{
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {// 序列化baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
}return null;
}
public static Object unserialize( byte[] bytes) {
ByteArrayInputStream bais = null;
try {
// 反序列化bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}return null;
}
}
4.我封裝了一個(gè)RedisServiceImpl類(lèi),主要是用對(duì)redis設(shè)值和取值
import com.ys.util.redis.SerializeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Service
public class RedisServiceImpl
{@Autowired
private StringRedisTemplate stringRedisTemplate;
@Autowired
private Jedis jedis;
public void setStr(String key, String value) {
setStr(key, value, null);
}
public void setStr(String key, Object value, Long time)
{if(value == null){
return;
}if(value instanceof String){
String obj = (String) value;
stringRedisTemplate.opsForValue().set(key, obj);
}else if(value instanceof List){
List obj = (List) value;
stringRedisTemplate.opsForList().leftPushAll(key,obj);
}else if(value instanceof Map){
Map obj = (Map) value;
stringRedisTemplate.opsForHash().putAll(key,obj);
}if (time != null)
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
}
public Object getKey(String key)
{return stringRedisTemplate.opsForValue().get(key);
}
public void delKey(String key) {
stringRedisTemplate.delete(key);
}
public boolean del(String key)
{return jedis.del(key.getBytes())>0;
}
}
5.測(cè)試redis是否ok,編寫(xiě)redisController類(lèi)
import com.ys.service.impl.RedisServiceImpl;
import com.ys.vo.IqProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
public class RedisServiceController
{
@Autowired
private RedisServiceImpl redisService;
@RequestMapping(value = "/setredis")
public String setredis(String keyredis){
redisService.setStr(keyredis,"2018年1月26日");
return "保存成功,請(qǐng)?jiān)L問(wèn)getredis查詢r(jià)edis";
}
@RequestMapping(value = "/setObj")
public String setObj(String keyredis){
IqProduct iqProduct = new IqProduct();
iqProduct.setSort(1);
iqProduct.setTimestamp(new Date().getTime());
iqProduct.setProductName("productname");
// list.add(iqProduct);
redisService.set(keyredis, iqProduct);
return "保存成功,請(qǐng)?jiān)L問(wèn)getredis查詢r(jià)edis";
}
@RequestMapping(value = "/getObj")
public Object getObj(String keyredis){
Object object = redisService.get(keyredis);
if(object !=null){
IqProduct iqProduct = (IqProduct) object;
System. out.println(iqProduct.getProductName());
System. out.println(iqProduct.getId());
System. out.println(iqProduct.getTimestamp());
}return object;
}
@RequestMapping(value = "/delObj")
public boolean delObj(String keyredis)
{boolean del = redisService.del(keyredis);
return del;
}
@RequestMapping(value = "/getredis")
public String getredis(String keyredis){
String getredis = (String) redisService.getKey(keyredis);
return "redis的key是===>"+getredis;
}
@RequestMapping(value = "/delredis")
public String delredis(String keyredis){
redisService.delKey(keyredis);
return "刪除成功,請(qǐng)通過(guò)getredis進(jìn)行查詢";
}
@RequestMapping(value = "/setList")
public String setList(String keyredis){
List list = new ArrayList();for (int i = 0;i10;i++){
IqProduct iqProduct = new IqProduct();
iqProduct.setSort(1);
iqProduct.setTimestamp(new Date().getTime());
iqProduct.setProductName("productname");
list.add(iqProduct);
}
redisService.set(keyredis, list);
return "保存成功,請(qǐng)?jiān)L問(wèn)getredis查詢r(jià)edis";
}
@RequestMapping(value = "/getList")
public Object getList(String keyredis){
Object object = redisService.get(keyredis);
if(object !=null){
ListIqProduct> iqProducts = (ListIqProduct>) object;
for (int i = 0;iiqProducts.size();i++){
IqProduct iqProduct = iqProducts.get(i);
System. out.println(iqProduct.getProductName());
System. out.println(iqProduct.getId());
System. out.println(iqProduct.getTimestamp());
}
}return object;
}
@RequestMapping(value = "/delList")
public boolean delList(String keyredis)
{
boolean del = redisService.del(keyredis);return del;
}
}
6.測(cè)試結(jié)果
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
您可能感興趣的文章:- Redis緩存-序列化對(duì)象存儲(chǔ)亂碼問(wèn)題的解決
- 淺談Redis存儲(chǔ)數(shù)據(jù)類(lèi)型及存取值方法
- redis 存儲(chǔ)對(duì)象的方法對(duì)比分析