RedisTemplate 使用
2017-05-04 00:00
513 查看
package org.spring.springboot.service.impl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spring.springboot.dao.CityDao; import org.spring.springboot.domain.City; import org.spring.springboot.service.CityService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.stereotype.Service; import java.util.List; import java.util.concurrent.TimeUnit; /** * 城市业务逻辑实现类 * <p> * Created by bysocket on 07/02/2017. */ @Service public class CityServiceImpl implements CityService { private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class); @Autowired private CityDao cityDao; @Autowired private RedisTemplate redisTemplate; /** * 获取城市逻辑: * 如果缓存存在,从缓存中获取城市信息 * 如果缓存不存在,从 DB 中获取城市信息,然后插入缓存 */ public City findCityById(Long id) { // 从缓存中获取城市信息 String key = "city_" + id; ValueOperations<String, City> operations = redisTemplate.opsForValue(); // 缓存存在 boolean hasKey = redisTemplate.hasKey(key); if (hasKey) { City city = operations.get(key); LOGGER.info("CityServiceImpl.findCityById() : 从缓存中获取了城市 >> " + city.toString()); return city; } // 从 DB 中获取城市信息 City city = cityDao.findById(id); // 插入缓存 operations.set(key, city, 10, TimeUnit.SECONDS); LOGGER.info("CityServiceImpl.findCityById() : 城市插入缓存 >> " + city.toString()); return city; } @Override public Long saveCity(City city) { return cityDao.saveCity(city); } /** * 更新城市逻辑: * 如果缓存存在,删除 * 如果缓存不存在,不操作 */ @Override public Long updateCity(City city) { Long ret = cityDao.updateCity(city); // 缓存存在,删除缓存 String key = "city_" + city.getId(); boolean hasKey = redisTemplate.hasKey(key); if (hasKey) { redisTemplate.delete(key); LOGGER.info("CityServiceImpl.updateCity() : 从缓存中删除城市 >> " + city.toString()); } return ret; } @Override public Long deleteCity(Long id) { Long ret = cityDao.deleteCity(id); // 缓存存在,删除缓存 String key = "city_" + id; boolean hasKey = redisTemplate.hasKey(key); if (hasKey) { redisTemplate.delete(key); LOGGER.info("CityServiceImpl.deleteCity() : 从缓存中删除城市 ID >> " + id); } return ret; } }
相关文章推荐
- 在ssm项目中使用redisTemplate
- Spring RedisTemplate使用多种operation
- 使用RedisTemplate的操作类访问Redis
- 10、Spring技术栈-整合Redis,使用RedisTemplate实现数据缓存实战
- 使用RedisTemplate 存储键值出现乱码
- 解决Spring Boot 使用RedisTemplate 存储键值出现乱码 \xac\xed\x00\x05t\x00
- StringRedisTemplate的使用(巨详细)
- RedisTemplate的使用说明(序列化以及事物)
- Spring集成Redis使用RedisTemplate
- 解决Spring Boot 使用RedisTemplate 存储键值出现乱码 \xac\xed\x00\x05t\x00
- 使用RedisTemplate进行Redis存取的工具类设计
- Spring Data Redis(StringRedisTemplate的使用)
- StringRedisTemplate的使用(巨详细)
- spring整合redis以及使用RedisTemplate的方法
- jedis与spring整合及简单的使用RedisTemplate操作
- RedisTemplate zSet的使用, 根据点赞排序,和创建时间排序2种方式
- RedisTemplate 的hash结构测试类, 使用场景 购物车
- Spring中使用RedisTemplate操作Redis(spring-data-redis)
- Spring集成Redis使用RedisTemplate
- 解决Spring Boot 使用RedisTemplate 存储键值出现乱码 \xac\xed\x00\x05t\x00