redis.clients
jedis
2.9.0
junit
junit
4.12
com.alibaba
fastjson
1.2.47
org.springframework.data
spring-data-redis
1.8.9.RELEASE
com.fasterxml.jackson.core
jackson-databind
2.8.2
package com.bb.pojo;
import java.io.Serializable;
/**
* Pet实体类
* @author lang
*
*/
//实现序列化
public class Pet implements Serializable{
private Integer pid;
private String pname;
private String color;
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public interface PetService {
//获取所有的宠物信息
List getPetInfo();
//删除宠物信息
int delPetInfo(Integer pid);
}
@Service
public class PetServiceImpl implements PetService{
//声明redisTemplate 对象
@Resource
private RedisTemplate redisTemplate;
String key = "petList";
//查询所有宠物信息
@Override
public List getPetInfo() {
//1.查询redis缓存库,如果有则直接返回
Object obj = redisTemplate.opsForValue().get(key);
if(obj!=null){
return (List) obj;
}
//2每有则查询msql数据库
System.out.println("========缓存没有,重新从mysql数据库查询=============");
List li = new ArrayList<>();
Pet p=new Pet();
p.setPid(1);
p.setPname("旺财");
p.setColor("黄色");
Pet p1=new Pet();
p1.setPid(2);
p1.setPname("旺财2");
p1.setColor("黄色2");
Pet p2=new Pet();
p2.setPid(3);
p2.setPname("旺财3");
p2.setColor("黄色3");
li.add(p);
li.add(p1);
li.add(p2);
//将数据缓存到redis中去
redisTemplate.opsForValue().set(key, li,30,TimeUnit.MINUTES);
//返回数据
return li;
}
//删除宠物信息
@Override
public int delPetInfo(Integer pid) {
//1、去mysql数据库中删除指定的宠物信息
//2.删除缓存的宠物信息
redisTemplate.delete(key);
return 0;
}
}
`
/**
* redis 原生方式存取数据
* @author lang
*
*/
public class TestSpring {
/**
* 测试连接
*/
@Test
public void testConnect(){
//获取spring 容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
//获取redisTemplate对象
RedisTemplate rt = (RedisTemplate) ac.getBean("redisTemplate");
//获取redisConnectfactory
RedisConnectionFactory connectionFactory = rt.getConnectionFactory();
RedisConnection connection = connectionFactory.getConnection();
System.out.println(connection);
}
//测试查询
@Test
public void testSelect(){
//获取Spring 容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
//获取业务层对象
PetService pe = (PetService) ac.getBean("petServiceImpl");
//验证查询
List petInfo = pe.getPetInfo();
System.out.println(petInfo);
}
/**
* 测试缓存同步
*/
@Test
public void testdelPet(){
//获取spring容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
//获取业务层对象
PetService pe = (PetService) ac.getBean("petServiceImpl");
//验证删除缓存
int delPetInfo = pe.delPetInfo(0);
System.out.println(delPetInfo);
}
redis.clients
jedis
2.9.0
org.springframework.data
spring-data-redis
1.8.9.RELEASE
junit
junit
4.12
com.fasterxml.jackson.core
jackson-databind
2.8.2
redis.clients
jedis
2.9.0
org.springframework.data
spring-data-redis
1.8.9.RELEASE
junit
junit
4.12
com.fasterxml.jackson.core
jackson-databind
2.8.2
< beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
"
default-autowire="byName">
/**
* 查询所有的宠物信息
*/
List getPetInfo();
/**
* 删除宠物信息
*/
int delPetInfo(Integer pid);
/**
* 增加宠物信息
* @param p
* @return
*/
Pet addPet(Pet p);
@CacheConfig(cacheNames={"petInfo","petList","petAA"})
@Service
public class PetServiceImpl implements PetService{
/**
* SpringCache的key的生产策略:
* 1、key不能重复
* 2、一个key有可能是多个方法在调用
* SpringEL表达式
* #root.methodName 使用当前方法的名字作为key
* #root.method.name 当前方法对象为key
* #root.target 当前被调用的对象为key
* #root.caches[0].name 使用@CacheConfig注解中声明的值为key
* #属性名.property 使用指定的形参的属性值为key
* #p0.property 使用指定的形参的属性值为key
*/
@Cacheable(key="#root.caches[0].name")
@Override
public List getPetInfo() {
//2、没有,则查询mysql数据库
System.out.println("=============缓存中没有,重新从数据库查询的=========");
List lp=new ArrayList<>();
Pet p=new Pet();
p.setPid(1);
p.setPname("旺财");
p.setColor("黄色");
Pet p1=new Pet();
p1.setPid(2);
p1.setPname("旺财2");
p1.setColor("黄色2");
Pet p2=new Pet();
p2.setPid(3);
p2.setPname("旺财3");
p2.setColor("黄色3");
lp.add(p);
lp.add(p1);
lp.add(p2);
//4、返回数据
return lp;
}
//删除宠物信息
@CacheEvict(key="#root.caches[0].name")
@Override
public int delPetInfo(Integer pid) {
//1、去mysql数据库中删除指定的宠物信息
return 0;
}
//增加宠物信息
@CacheEvict(key="#root.caches[0].name")
@Cacheable(key="#p0.pid")
@Override
public Pet addPet(Pet p) {
System.out.println("============增加宠物信息,并缓存===========");
return p;
}
//更新宠物信息
@CacheEvict(key="#root.caches[0].name")
@CachePut(key="#p0.pid")
@Override
public Pet updatePet(Pet p) {
System.out.println("============更新宠物信息,并缓存===========");
return p;
}
* 测试链接
*
*/
@Test
public void testConnect(){
//获取Spring容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
//获取RedisTemplate对象
RedisTemplate rt= (RedisTemplate) ac.getBean("redisTemplate");
//获取RedisConnectFactory
RedisConnectionFactory connectionFactory = rt.getConnectionFactory();
RedisConnection connection = connectionFactory.getConnection();
System.out.println(connection);
}
/**
* 测试查询
*/
@Test
public void testSelect(){
//获取Spring容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
//获取业务层对象
PetService ps=(PetService) ac.getBean("petServiceImpl");
//验证查询
List petInfo = ps.getPetInfo();
System.out.println(petInfo);
}
/**
* 测试删除:缓存同步
*/
@Test
public void testDelete(){
//获取Spring容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationcontext.xml");
//获取业务层对象
PetService ps=(PetService) ac.getBean("petServiceImpl");
//验证查询
int delPetInfo = ps.delPetInfo(0);
}