com.base4j.cache.EhRedisCache Maven / Gradle / Ivy
package com.base4j.cache;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.concurrent.Callable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.support.SimpleValueWrapper;
import net.sf.ehcache.Element;
public class EhRedisCache implements Cache {
private static final Logger LOG = LoggerFactory.getLogger(EhRedisCache.class);
private String name;
private net.sf.ehcache.Cache ehCache;
private long liveTime = 1 * 60 * 60; // 默认1h=1*60*60
@Autowired
private Base4jCacheManager cacheManager;
@Override
public String getName() {
return this.name;
}
@Override
public Object getNativeCache() {
return this;
}
@Override
public ValueWrapper get(Object key) {
// cacheManager.get(region, key);
Element value = ehCache.get(key);
LOG.info("Cache L1 (ehcache) :{}={}", key, value);
if (value != null) {
return (value != null ? new SimpleValueWrapper(value.getObjectValue()) : null);
}
// TODO 这样会不会更好?访问10次EhCache 强制访问一次redis 使得数据不失效
final String keyStr = key.toString();
return null;
}
@Override
public void put(Object key, Object value) {
ehCache.put(new Element(key, value));
final String keyStr = key.toString();
final Object valueStr = value;
}
@Override
public void evict(Object key) {
ehCache.remove(key);
final String keyStr = key.toString();
}
@Override
public void clear() {
ehCache.removeAll();
}
public net.sf.ehcache.Cache getEhCache() {
return ehCache;
}
public void setEhCache(net.sf.ehcache.Cache ehCache) {
this.ehCache = ehCache;
}
public long getLiveTime() {
return liveTime;
}
public void setLiveTime(long liveTime) {
this.liveTime = liveTime;
}
public void setName(String name) {
this.name = name;
}
/**
* 描述 : Object转byte[].
*
* @param obj
* @return
*/
private byte[] toByteArray(Object obj) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
bytes = bos.toByteArray();
oos.close();
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return bytes;
}
/**
* 描述 : byte[]转Object .
*
* @param bytes
* @return
*/
private Object toObject(byte[] bytes) {
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
obj = ois.readObject();
ois.close();
bis.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
@Override
public T get(Object key, Class type) {
// TODO Auto-generated method stub
return null;
}
@Override
public T get(Object key, Callable valueLoader) {
// TODO Auto-generated method stub
return null;
}
@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
// TODO Auto-generated method stub
return null;
}
@Override
public Object gets(Object key) throws CacheException {
// TODO Auto-generated method stub
return null;
}
@Override
public void evict(List keys) throws CacheException {
// TODO Auto-generated method stub
}
@Override
public List keys() throws CacheException {
// TODO Auto-generated method stub
return null;
}
@Override
public void destroy() throws CacheException {
// TODO Auto-generated method stub
}
}