@Slf4j
public abstract class DataLoader<T> {
public abstract T getObjectFromCache(IdPin idPin);
public abstract T getObjectFromDb(IdPin idPin);
public abstract void setObjectToCache(IdPin idPin, T Object);
public T getObject(IdPin idPin, DataLoaderStrategyEnum dataLoadStrategyEnum) {
T object = null;
//从cache中取
if (DataLoaderStrategyEnum.CACHE.equals(dataLoadStrategyEnum) || DataLoaderStrategyEnum.CACHE_DB.equals(dataLoadStrategyEnum)) {
object = this.getObjectFromCache(idPin);
if (null != object) {
TrackContext.appendTrackInfo(String.format("redisGet blog from Redis success,id %s", idPin.getId()));
return object;
}
}
//从持久化介质取
if (DataLoaderStrategyEnum.CACHE_DB.equals(dataLoadStrategyEnum)) {
//从db取,如果取到放进缓存
object = this.getObjectFromDb(idPin);
TrackContext.appendTrackInfo(String.format("redisGet blog from db success,id %s", idPin.getId()));
//存放Cache
if (null != object) {
this.setObjectToCache(idPin, object);
TrackContext.appendTrackInfo(String.format("redisSet blog to Redis success,id %s", idPin));
}
}
return object;
}
}
评论