package grp.pt.common.bs;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;
import grp.pt.common.ICacheService;
import grp.pt.util.PropertyUtil;
import grp.pt.util.cache.CacheKeyConstant;
import grp.pt.util.model.Session;
@CacheConfig(cacheNames="common-caches")
@Repository
public class CacheService implements ICacheService{
private static final Log logger = LogFactory.getLog(CacheService.class);
@Autowired
private RedisTemplate<Object,Object> rt;
private String getCacheKey(Session session, String key) {
if(session == null) {
return key;
}
String cacheKey = session.getTenantId()+"#"+session.getBusiYear()+"#"+key;
return cacheKey;
}
private String getYearKey(int year, String key){
return year+key;
}
private String getRgYearKey(Session session, String key){
StringBuffer keyStringBuffer = new StringBuffer();
keyStringBuffer.append(session.getTenantId()).append(session.getBusiYear()).append(key);
return keyStringBuffer.toString();
}
public Object get(Session session, String key) {
String cacheKey = getCacheKey(session, key);
return this.getFromCache(cacheKey);
}
private Object getFromCache(String realKey){
return rt.opsForValue().get(realKey);
}
public void put(Session session, String key, Object value) {
String cacheKey = getCacheKey(session, key);
this.putToCache(cacheKey, value);
}
private Object putToCache(String realKey,Object value){
rt.opsForValue().set(realKey, value);
return value;
}
public boolean remove(Session session, String key) {
String cacheKey = getCacheKey(session, key);
return this.removeFromCache(cacheKey);
}
private boolean removeFromCache(String realKey){
rt.delete(realKey);
return true;
}
public void removeAll(String key) {
rt.delete(keys(key));
}
public Set<Object> keys(String key) {
return rt.keys(key);
}
public Object getMap(Session session, String key, Object key2) {
String cacheKey = getCacheKey(session, key);
return rt.opsForHash().get(cacheKey, key2);
}
public Map getMap(Session session, String key) {
String cacheKey = getCacheKey(session, key);
return rt.opsForHash().entries(cacheKey);
}
public List<Object> getMapForList(Session session, String key) {
String cacheKey = getCacheKey(session, key);
return rt.opsForHash().values(cacheKey);
}
public void putMap(Session session, String key, Map value) {
String cacheKey = getCacheKey(session, key);
removeFromCache(cacheKey);
rt.opsForHash().putAll(cacheKey, value);
}
public void putMapToAdd(Session session, String key, Map value) {
String cacheKey = getCacheKey(session, key);
rt.opsForHash().putAll(cacheKey, value);
}
public void putMap(Session session, String key, Object key2, Object value) {
String cacheKey = getCacheKey(session, key);
rt.opsForHash().put(cacheKey, key2, value);
}
public Long removeMap(Session session, String key, Object key2) {
String cacheKey = getCacheKey(session, key);
return rt.opsForHash().delete(cacheKey, key2);
}
public void rightPush(Session session, String key, Object value) {
String cacheKey = getCacheKey(session, key);
rt.opsForList().rightPush(cacheKey, value);
}
public void leftPush(Session session, String key, Object value) {
String cacheKey = getCacheKey(session, key);
rt.opsForList().leftPush(cacheKey, value);
}
public void rightPushAll(Session session, String key, Object ...values) {
String cacheKey = getCacheKey(session, key);
rt.opsForList().rightPushAll(cacheKey, values);
}
public void rightPushAll(Session session, String key, Object collection) {
String cacheKey = getCacheKey(session, key);
rt.opsForList().rightPushAll(cacheKey, collection);
}
public long listSize(Session session, String key){
String cacheKey = getCacheKey(session, key);
return rt.opsForList().size(cacheKey);
}
public Object leftPop(Session session, String key) {
String cacheKey = getCacheKey(session, key);
return rt.opsForList().leftPop(cacheKey);
}
public Object rightPop(Session session, String key) {
String cacheKey = getCacheKey(session, key);
return rt.opsForList().rightPop(cacheKey);
}
public Object leftPop(Session session,String key, long timeout, TimeUnit unit) {
String cacheKey = getCacheKey(session, key);
return rt.opsForList().leftPop(cacheKey, timeout, unit);
}
public Long setAdd(Session session,String key,Object ...values) {
String cacheKey = getCacheKey(session, key);
return rt.opsForSet().add(cacheKey, values);
}
public Boolean isSetMember(Session session,String key,Object value) {
String cacheKey = getCacheKey(session, key);
return rt.opsForSet().isMember(cacheKey, value);
}
public Set<Object> setMembers(Session session,String key) {
String cacheKey = getCacheKey(session, key);
return rt.opsForSet().members(cacheKey);
}
public boolean keyExists(Session session, String key) {
String cacheKey = getCacheKey(session, key);
try {
return rt.hasKey(cacheKey);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public String keyType(String key) {
DataType type = rt.type(key);
return type.code();
}
public boolean expire(String key,long time){
try {
if(time>0){
rt.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public long getExpire(String key){
return rt.getExpire(key,TimeUnit.SECONDS);
}
public Object getNoRg(int year, String key) {
return this.getFromCache(getYearKey(year, key));
}
public void putNoRg(int year, String key, Object value) {
this.putToCache(getYearKey(year, key), value);
}
public boolean removeNoRg(int year, String key) {
return this.removeFromCache(getYearKey(year, key));
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List saveToCache(Session se, List objectsToCache, String keyPrefix, String identityPropertyName){
List list=new ArrayList();
for(Object o : objectsToCache){
Object id=PropertyUtil.getPropertySmart(o, identityPropertyName);
list.add(id);
this.put(se, keyPrefix+id, o);
}
this.put(se, keyPrefix+CacheKeyConstant.LIST, list);
return list;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Object> getIdList(Session se, String keyPrefix){
return (List)this.get(se, keyPrefix+CacheKeyConstant.LIST);
}
@SuppressWarnings("rawtypes")
public void setIdList(Session se, String keyPrefix,List list){
this.put(se, keyPrefix+CacheKeyConstant.LIST,list);
}
@SuppressWarnings("rawtypes")
public List getObjectList(Session se, String keyPrefix){
List<Object> list = new ArrayList<Object>();
List<Object> idlist = this.getIdList(se, keyPrefix);
for(int i=0; i < idlist.size(); i++){
Object id=idlist.get(i);
list.add(this.get(se, keyPrefix+id));
}
return list;
}
@Override
public Boolean setRemove(Session session, String key, Object... values) {
String cacheKey = getCacheKey(session, key);
if(!keyExists(session, key)) {
return true;
}
if(values == null || values.length <= 0) {
return remove(session, key);
}
return rt.opsForSet().remove(cacheKey, values) > 0;
}
@Override
public List<Object> getListrange(Session session, String key, long start, long end) {
String cacheKey = getCacheKey(session, key);
return rt.opsForList().range(cacheKey, start, end);
}
}