版权声明:版权??? 版权.... 版权!!! 什么样的事要说三遍 https://blog.csdn.net/qq_16513911/article/details/82874464
(一) 内容简介,父类以及子类结构,重点在于通过@Service实现类,判断类型来new出不同的缓存实现类
首先要写一个缓存接口CacheService,来统一规定缓存的必要操作.
public interface CacheService {
//当前缓存的类型
String getType();
//清空缓存内容
void clear();
void putString(String key, String value);
void putString(String key, String value, int seconds);
String getString(String key);
void putObject(String key, Object value) throws IOException;
void putObject(String key, Object value, int seconds) throws IOException;
<T> T getObject(String key, Class<T> clazz) throws IOException;
<T> List<T> getList(String key, Class<T> clazz) throws IOException;
<T> PageInfo<T> getPageList(String key, Class<T> clazz) throws IOException;
}
接下来写@service实现类,看类的调用过程,每次都会判断一下使用了哪个类型的缓存,当然这两个缓存也都实现了CacheService接口,只要以此按照自己的方法实现就行了
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import com.github.pagehelper.PageInfo;
import com.w3china.mingjing3.dydata.DataSourceContextHolder;
import com.w3china.mingjing3.model.SiteSettings;
import com.w3china.mingjing3.web.Constants;
import com.w3china.mingjing3.web.service.CacheService;
@Service
public class CacheServiceImpl implements CacheService {
private Map<String, CacheService> cacheMap = new HashMap<>();
private CacheService getCacheService() {
//从上下文中取得配置
String attribute = (String) RequestContextHolder.currentRequestAttributes().getAttribute(Constants.SETTINGS, RequestAttributes.SCOPE_REQUEST);
if (!cacheMap.containsKey(attribute)) {
//根据内容判断缓存类型
if ("redis".equalsIgnoreCase(attribute)){
cacheMap.put(attribute, new RedisCacheService());
} else {
cacheMap.put(attribute, new MemoryCacheService());
}
}
return cacheMap.get(attribute);
}
public String getType() {
return "";
}
public void clear() {
// do noting.
}
@Override
public String getString(String key) {
return getCacheService().getString(key);
}
@Override
public void putString(String key, String value) {
putString(key, value, 60);
}
@Override
public void putString(String key, String value, int seconds) {
getCacheService().putString(key, value, seconds);
}
@Override
public <T> T getObject(String key, Class<T> clazz) throws IOException {
return getCacheService().getObject(key, clazz);
}
@Override
public <T> List<T> getList(String key, Class<T> clazz) throws IOException {
return getCacheService().getList(key, clazz);
}
@Override
public <T> PageInfo<T> getPageList(String key, Class<T> clazz) throws IOException {
return getCacheService().getPageList(key, clazz);
}
@Override
public void putObject(String key, Object value) throws IOException {
putObject(key, value, 60);
}
@Override
public void putObject(String key, Object value, int seconds) throws IOException {
getCacheService().putObject(key, value, seconds);
}
}
举例放一个RedisCacheService类吧,这里面用了一下序列化 (还有一个MemoryCacheService类里面用的是Map实现了类似Rides效果,也打算另写一篇文章) (还有Redis初始化配置,我打算单独写一篇.)
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.w3china.mingjing3.model.SiteSettings;
import com.w3china.mingjing3.web.service.CacheService;
import redis.clients.jedis.JedisPoolConfig;
public class RedisCacheService implements CacheService {
private static final Logger logger = LoggerFactory.getLogger(RedisCacheService.class);
private StringRedisTemplate redisTemplate;
//通过构造方法配置Redis 略,会单写一篇文章记录一下
private ObjectMapper om;
private ObjectMapper getObjectMapper() {
if (om == null) {
om = new ObjectMapper();
om.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
return om;
}
@Override
public String getType() {
return "redis";
}
@Override
public void clear() {
// do nothing.
}
@Override
public String getString(String key) {
if (redisTemplate.hasKey(key)) {
return redisTemplate.opsForValue().get(key);
}
return null;
}
@Override
public void putString(String key, String value) {
putString(key, value, 60);
}
@Override
public void putString(String key, String value, int seconds) {
if (value != null) {
redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
}
}
@Override
public <T> T getObject(String key, Class<T> clazz) throws IOException {
String val = getString(key);
if (val == null)
return null;
return getObjectMapper().readValue(val, clazz);
}
@Override
public <T> List<T> getList(String key, Class<T> clazz) throws IOException {
ObjectMapper om = getObjectMapper();
String val = getString(key);
if (val == null)
return null;
JavaType javaType1 = om.getTypeFactory().constructParametricType(List.class, clazz);
return getObjectMapper().readValue(val, javaType1);
}
@Override
public <T> PageInfo<T> getPageList(String key, Class<T> clazz) throws IOException {
ObjectMapper om = getObjectMapper();
String val = getString(key);
if (val == null)
return null;
JavaType javaType1 = om.getTypeFactory().constructParametricType(PageInfo.class, clazz);
return getObjectMapper().readValue(val, javaType1);
}
@Override
public void putObject(String key, Object value) throws IOException {
putObject(key, value, 60);
}
@Override
public void putObject(String key, Object value, int seconds) throws IOException {
String val = getObjectMapper().writeValueAsString(value);
putString(key, val, seconds);
}
}