Jedis+Redis+spring缓存

Posted 流星~泪雨

tags:

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

Redis程序使用它?
Jedis 访问redis java api

Redis-server & //后台运行
防火墙要关闭


ts-parent的pom.xml加上jedis依赖
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
</dependency>

编写测试类
package jedis;

import org.junit.Test;

import redis.clients.jedis.Jedis;

public class TestJedis {
@Test
public void jedis(){
//连接redis服务器,ip+port
String ip = "192.168.27.113";
//获取到jedis对象
Jedis jedis = new Jedis(ip, 6379);
//调用redis set,key=name,value=tony
//jedis.set("name", "tony");
System.out.println(jedis.get("name"));
}
}

 

package jedis;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

public class TestJedis {
@Test
public void jedis(){
//连接redis服务器,ip+port
String ip = "192.168.27.113";
//获取到jedis对象
Jedis jedis = new Jedis(ip, 6379);
//调用redis set,key=name,value=tony
//jedis.set("name", "tony");
System.out.println(jedis.get("name"));
}

@Test //分片Shard池化
public void sharded(){
//创建一个Sharded池配置对象
JedisPoolConfig config = new JedisPoolConfig();
//最大的连接数
config.setMaxTotal(50);

//连接多个redis节点,ip和port的信息
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
//一个的节点信息就ok
JedisShardInfo info1 =
new JedisShardInfo("192.168.27.113",6379);
shards.add(info1);

//创建分片池
ShardedJedisPool pool =
new ShardedJedisPool(config, shards);

//从池中获取一个jedis链接
ShardedJedis jedis = pool.getResource();
System.out.println(jedis.get("name"));
}
}


把jedis和spring框架整合
通过xml配置把jedis的对象创建交给spring框架。

在哪里使用缓存?
package cn.tedu.store.service;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tedu.store.bean.dict.Area;
import cn.tedu.store.bean.dict.City;
import cn.tedu.store.bean.dict.Province;
import cn.tedu.store.mapper.DictMapper;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Service("dictService")
public class DictServiceImpl implements DictService {

@Resource
private DictMapper dictMapper;
//获取spring框架创建的jedis对象
@Resource
private ShardedJedisPool shardedJedisPool;
//转换对象到json或者json到java对象工具类
private static final ObjectMapper MAPPER = new ObjectMapper();

public List<Province> getProvinceList() {
return dictMapper.getProvinceList();
}

public List<City> getCityList(String provinceCode){
List<City> cityList = null;
//设置redis key的规则
String KEY = "TS_CITY_"+provinceCode;

//由池中获取jedis对象
ShardedJedis jedis = shardedJedisPool.getResource();

//1.从redis中获取数据,如果有数据直接返回
//注意set设置2次就会被覆盖
if(jedis.exists(KEY)){ //判断key是否存在
String json = jedis.get(KEY);
JsonNode jsonNode;
try {
//从缓存中获取数据,然后把json转成java对象,然后直接返回,无需访问数据库
jsonNode = MAPPER.readTree(json);
Object obj = MAPPER.readValue(jsonNode.traverse(),
MAPPER.getTypeFactory().constructCollectionType(List.class, City.class));
return (List<City>) obj; //减轻数据库访问压力
} catch (Exception e) {
e.printStackTrace();
}
}else{
//传统方式直接从数据库读取数据
cityList = dictMapper.getCityList(provinceCode);

//2.第一访问时redis没有值,必须从数据库中获取值后,把值放入redis
//把java list集合转成字符串json
try {
String json = MAPPER.writeValueAsString(cityList);
jedis.set(KEY, json);
return cityList;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}

public List<Area> getAreaList(String cityCode) {
return dictMapper.getAreaList(cityCode);
}

public String getProvinceNameByCode(String provinceCode) {
return dictMapper.getProvinceNameByCode(provinceCode);
}

public String getCityNameByCode(String cityCode) {
return dictMapper.getCityNameByCode(cityCode);
}

public String getAreaNameByCode(String areaCode) {
return dictMapper.getAreaNameByCode(areaCode);
}

}

Redis缓存应用注意点:
1、 是不是所有的数据都能放在缓存中?
Reids能否替代mysql
当然不是,
缓存是存放热点数据,不会频繁修改的数据!!
内存掉电会有什么问题?
数据丢失。Redis每秒钟会自动存盘c++。MemCache

 

以上是关于Jedis+Redis+spring缓存的主要内容,如果未能解决你的问题,请参考以下文章

Spring boot + Mybatis plus + Redis实现二级缓存

spring整合redis

Spring Data JPA整合Redis缓存的配置

Redis + Jedis + Spring整合遇到的异常(转)

使用Jedis操作redis 缓存

spring-data-redis multiGet 如何处理缓存未命中?