过期时间类
public class MememCacheEntity {
private Date _addDate = null;
public Date getAddDate()
{
return this._addDate;
}
public void setAddDate(Date newDate)
{
this._addDate = newDate;
}
private List<MyT> _data = null;
public List<MyT> getData()
{
return this._data;
}
public void setData(List<MyT> newData)
{
this._data = newData;
}
private String _key = null;
public String getKey()
{
return this._key;
}
public void setKey(String newKey)
{
this._key = newKey;
}
}
对缓存数据进行操作
public class MemCahceHelper {
private HashMap<String,MememCacheEntity> cacheData = null;
private static MemCahceHelper _Instance = null;
public static MemCahceHelper Instance() {
if(_Instance == null)
{
_Instance = new MemCahceHelper();
}
return _Instance;
}
private MemCahceHelper() {
cacheData = new HashMap<String,MememCacheEntity>();
}
public void Add(String key, List<MyT> data,int expireDate)
{
if(key != "" && key != null)
{
if(cacheData.containsKey(key)) {
cacheData.remove(key);
}
MememCacheEntity newData = new MememCacheEntity();
newData.setData(data);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
newData.setAddDate(new Date(Long.parseLong(String.valueOf(new Date().getTime()+expireDate))));
newData.setKey(key);
cacheData.put(key,newData);
}
}
public void Remove(String key)
{
if(key !="" && key !=null)
{
if(cacheData.containsKey(key)) {
cacheData.remove(key);
}
}
}
public List<MyT> GetCache(String key)
{
List<MyT> tempData = null;
if(key !="" && key !=null)
{
if(cacheData.containsKey(key)) {
System.out.println("进入缓存--------" + key);
MememCacheEntity data = cacheData.get(key);
if(data.getAddDate().getTime() > new Date().getTime()) {
tempData = data.getData();
}else{
System.out.println("删除缓存数据--重新读取数据库数据");
MemCahceHelper.Instance().Remove(key);
}
}
}
return tempData;
}
}
Mapper接口
public List<MyT> CacheaiteshopList();
Mapper.xml
<select id="CacheaiteshopList" resultType="com.demo.entity.AiteshopCarClass">
SELECT * FROM aiteshop_car_class
</select>
Service
public List<MyT> CacheaiteshopList();
ServiceImpl
@Override
public List<MyT> CacheaiteshopList(){
List<MyT> aiteshopCarClassList = null;
if(MemCahceHelper.Instance().GetCache(currencyKey)!=null){
aiteshopCarClassList = MemCahceHelper.Instance().GetCache(currencyKey);
}
if(aiteshopCarClassList == null)
{
List<MyT> myTS = this.baseMapper.CacheaiteshopList();
aiteshopCarClassList = myTS;
List<MyT> parentCurrrencyList = new ArrayList<MyT>(aiteshopCarClassList);
MemCahceHelper.Instance().Add(currencyKey,parentCurrrencyList,10000);
}
return aiteshopCarClassList;
}
Controller
@RequestMapping(value = "/index")
public String AiteshopCarClassIndex(HttpServletRequest request){
List<MyT> aiteshopCarClassList = aiteshopCarClassService.CacheaiteshopList();
request.setAttribute("aiteshopCarClassList", aiteshopCarClassList);
return "index";
}