Redis(1)安装 & 配置
Redis(2)基本键值操作
Redis(3)常用维护操作
Redis(4)客户端连接:Java
Spring 集成 Reids(Spring-data-redis)
...
// 依赖部分
dependencies {
//redis-jedis 依赖
compile 'redis.clients:jedis:2.9.0'
//spring-data-redis 依赖
compile 'org.springframework.data:spring-data-redis:2.0.3.RELEASE'
//spring core 依赖
compile "org.springframework:spring-beans:4.3.14.RELEASE"
compile "org.springframework:spring-core:4.3.14.RELEASE"
compile "org.springframework:spring-context:4.3.14.RELEASE"
//spring test 依赖
compile "org.springframework:spring-test:4.3.14.RELEASE"
testCompile "junit:junit:4.12"
//log4j 依赖
compile 'org.slf4j:slf4j-nop:1.7.25'
compile "org.apache.logging.log4j:log4j-core:2.9.0"
compile "org.apache.logging.log4j:log4j-api:2.9.0"
}
...
<beans ...>
<context:component-scan base-package="site.assad.service" />
<context:component-scan base-package="site.assad.dao" />
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"
p:maxIdle="300"
p:maxTotal="1000"
p:maxWaitMillis="1000"
p:testOnBorrow="true"
p:testOnReturn="true"/>
<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy"
p:host-name="127.0.0.1"
p:port="6379"
p:password="assad"
p:usePool="true"
p:poolConfig-ref="jedisPoolConfig"/>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
p:connectionFactory-ref="jedisConnectionFactory" />
beans>
package site.assad.domain;
public class User implements Serializable{
private static final long serialVersionUID = 23333L;
private int id;
private String name;
private String password;
private String icon;
// getter,setter,toString,constructor
}
package site.assad.service;
public class UserService {
private UserDao userDao;
//自动注入 RedisTemplate 对象
private RedisTemplate<String,Object> redisTemplate;
private final static Logger log = LogManager.getLogger();
//获取 user
public User getUser(int userId) {
//当 redis 缓存中存在 user,返回该 user
User userCache = (User) redisTemplate.opsForValue().get(userId+"");
if (userCache != null)
return userCache;
//当 redis 缓存中不存在 user,从数据库中获取 redis ,同时将 user 写入缓存
log.info("real query user from DB");
User user = userDao.getUserById(userId);
if(user != null )
redisTemplate.opsForValue().set(user.getId()+"",user);
return user;
}
//将 userId 对应的 User 从缓存中删除
public void removeUserFromCache(int userId){
//缓存中删除 user
redisTemplate.delete(userId+"");
log.info("remove user "+ userId + " from cache");
}
//删除 user
public void removeUser(int userId){
removeUserFromCache(userId);
userDao.deleteUserById(userId);
log.info("remove user "+ userId + " from database");
}
//添加 user
public void addUser(User user){
if(userDao.addUser(user))
redisTemplate.opsForValue().set(user.getId()+"",user);
}
//更新 user
public void updateUser(User user){
if(userDao.update(user))
redisTemplate.opsForValue().set(user.getId()+"",user);
}
}
SpringJUnit4ClassRunner.class) (
locations = {"classpath:applicationContext.xml"}) (
public class UserServcieTest {
private UserService userService;
private RedisTemplate redisTemplate;
private final static Logger log = LogManager.getLogger();
.....
//测试删除缓存队列
public void testRemoveUserFromCache(){
for(int i=1;i<5;i++){
log.info("query get User(userId=1)"+i+" ...");
log.info(userService.getUser(1));
}
userService.removeUserFromCache(1);
log.info("query get User(userId=1)"+6+" ...");
log.info(userService.getUser(1));
}
.....
}
02:01:13.642 [main] INFO site.assad.service.UserServcieTest - query get User(userId=1)1 ...
02:01:13.676 [main] INFO site.assad.service.UserService - real query user from DB
02:01:13.678 [main] INFO site.assad.service.UserServcieTest - User{id=1, name='vancy', password='44455', icon='icon-002'}
02:01:13.678 [main] INFO site.assad.service.UserServcieTest - query get User(userId=1)2 ...
02:01:13.680 [main] INFO site.assad.service.UserServcieTest - User{id=1, name='vancy', password='44455', icon='icon-002'}
02:01:13.681 [main] INFO site.assad.service.UserServcieTest - query get User(userId=1)3 ...
02:01:13.681 [main] INFO site.assad.service.UserServcieTest - User{id=1, name='vancy', password='44455', icon='icon-002'}
02:01:13.681 [main] INFO site.assad.service.UserServcieTest - query get User(userId=1)4 ...
02:01:13.684 [main] INFO site.assad.service.UserServcieTest - User{id=1, name='vancy', password='44455', icon='icon-002'}
02:01:13.686 [main] INFO site.assad.service.UserService - remove user 1 from cache
02:01:13.686 [main] INFO site.assad.service.UserServcieTest - query get User(userId=1)6 ...
02:01:13.686 [main] INFO site.assad.service.UserService - real query user from DB
02:01:13.686 [main] INFO site.assad.service.UserServcieTest - User{id=1, name='vancy', password='44455', icon='icon-002'}