
com.github.edgar615.util.spring.jdbc.CacheWrappedJdbc Maven / Gradle / Ivy
package com.github.edgar615.util.spring.jdbc;
import com.google.common.collect.Lists;
import com.github.edgar615.util.db.Jdbc;
import com.github.edgar615.util.db.Persistent;
import com.github.edgar615.util.search.Example;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import java.util.List;
import java.util.Map;
/**
* Created by Edgar on 2018/5/24.
*
* @author Edgar Date 2018/5/24
*/
public class CacheWrappedJdbc implements Jdbc {
private final Jdbc jdbc;
private final CacheManager cacheManager;
public CacheWrappedJdbc(Jdbc jdbc, CacheManager cacheManager) {
this.jdbc = jdbc;
this.cacheManager = cacheManager;
}
@Override
public void insert(Persistent persistent) {
jdbc.insert(persistent);
}
@Override
public void insertAndGeneratedKey(Persistent persistent) {
jdbc.insertAndGeneratedKey(persistent);
}
@Override
public > int deleteById(Class elementType, ID id) {
int result = jdbc.deleteById(elementType, id);
evictCache(elementType, id);
return result;
}
@Override
public > int deleteByExample(Class elementType, Example example) {
int result = jdbc.deleteByExample(elementType, example);
clearCache(elementType);
return result;
}
@Override
public int updateById(Persistent persistent, Map addOrSub,
List nullFields, ID id) {
int result = jdbc.updateById(persistent, addOrSub, nullFields, id);
evictCache(persistent.getClass(), id);
return result;
}
@Override
public int updateByExample(Persistent persistent, Map addOrSub,
List nullFields, Example example) {
int result = jdbc.updateByExample(persistent, addOrSub, nullFields, example);
clearCache(persistent.getClass());
return result;
}
@Override
public > T findById(Class elementType, ID id,
List fields) {
Cache cache = cacheManager.getCache(cacheName(elementType));
if (cache == null) {
return jdbc.findById(elementType, id, fields);
}
T persistent = null;
Cache.ValueWrapper valueWrapper = cache.get(id);
if (valueWrapper != null) {
persistent = (T) valueWrapper.get();
} else {
persistent = jdbc.findById(elementType, id, Lists.newArrayList());
if (persistent == null) {
return null;
}
cache.put(id, persistent);
}
if (fields == null || fields.isEmpty()) {
return persistent;
}
Map map = persistent.toMap();
fields.stream().forEach(f -> map.remove(f));
Persistent newPersistent = newDomain(elementType);
newPersistent.fromMap(map);
return (T) newPersistent;
}
@Override
public > List findByExample(Class elementType,
Example example) {
return jdbc.findByExample(elementType, example);
}
@Override
public > List findByExample(Class elementType, Example example,
int start, int limit) {
return jdbc.findByExample(elementType, example, start, limit);
}
@Override
public > int countByExample(Class elementType, Example example) {
return jdbc.countByExample(elementType, example);
}
private void evictCache(Class elementType, Object id) {
Cache cache = cacheManager.getCache(cacheName(elementType));
if (cache != null) {
cache.evict(id);
}
}
private void clearCache(Class elementType) {
Cache cache = cacheManager.getCache(cacheName(elementType));
if (cache != null) {
cache.clear();
}
}
private Persistent newDomain(Class extends Persistent> clazz) {
try {
return clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private String cacheName(Class elementType) {
return elementType.getSimpleName() + "JdbcCache";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy