Skip to content

Commit

Permalink
Change 缓存使用用EhcacheDao而是使用其接口
Browse files Browse the repository at this point in the history
  • Loading branch information
enilu committed Jan 3, 2020
1 parent 698fd54 commit 4070a18
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 38 deletions.
3 changes: 2 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
editLinks: true,
editLinkText: '编辑此页面!',
nav: [
{text: '指南', link: '/'},
{text: '指南', link: '/base/preface'},
{text: '功能',
items:[
{text: '国际化',link:'/action/i18n'},
Expand Down Expand Up @@ -76,6 +76,7 @@ module.exports = {
'/feature/exportExcel',
'/feature/messageCenter',
'/feature/cms.md'

]
},
{
Expand Down
18 changes: 9 additions & 9 deletions flash-api/src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="true" >

<diskStore path="java.io.tmpdir/ehcache"/>

<defaultCache
Expand Down Expand Up @@ -32,8 +32,8 @@
<!--SESSION缓存-->
<cache name="SESSION"
maxElementsInMemory="50000"
timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"
eternal="true"
clearOnFlush="false"
overflowToDisk="true"
Expand Down Expand Up @@ -65,21 +65,21 @@

<!--
maxElementsInMemory="10000" //Cache中最多允许保存的数据对象的数量
external="false" //缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期
external="false" //缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期
timeToLiveSeconds="3600" //缓存的存活时间,从开始创建的时间算起
timeToIdleSeconds="3600" //多长时间不访问该缓存,那么ehcache 就会清除该缓存
timeToIdleSeconds="3600" //多长时间不访问该缓存,那么ehcache 就会清除该缓存
这两个参数很容易误解,看文档根本没用,我仔细分析了ehcache的代码。结论如下:
1、timeToLiveSeconds的定义是:以创建时间为基准开始计算的超时时长;
2、timeToIdleSeconds的定义是:在创建时间和最近访问时间中取出离现在最近的时间作为基准计算的超时时长;
3、如果仅设置了timeToLiveSeconds,则该对象的超时时间=创建时间+timeToLiveSeconds,假设为A;
4、如果没设置timeToLiveSeconds,则该对象的超时时间=min(创建时间,最近访问时间)+timeToIdleSeconds,假设为B;
5、如果两者都设置了,则取出A、B最少的值,即min(A,B),表示只要有一个超时成立即算超时。
overflowToDisk="true" //内存不足时,是否启用磁盘缓存
overflowToDisk="true" //内存不足时,是否启用磁盘缓存
diskSpoolBufferSizeMB //设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区
maxElementsOnDisk //硬盘最大缓存个数
diskPersistent //是否缓存虚拟机重启期数据The default value is false
diskPersistent //是否缓存虚拟机重启期数据The default value is false
diskExpiryThreadIntervalSeconds //磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy="LRU" //当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush //内存数量最大时是否清除
Expand Down
5 changes: 3 additions & 2 deletions flash-core/src/main/java/cn/enilu/flash/cache/CacheDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import java.io.Serializable;

/**
* CacheDao
* 缓存接口
*
* @author enilu
* @version 2018/9/12 0012
*/
public interface CacheDao {

String CONSTANT = "CONSTANT";
String SESSION = "SESSION";

/**
* 设置hash key值
Expand Down
15 changes: 7 additions & 8 deletions flash-core/src/main/java/cn/enilu/flash/cache/TokenCache.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cn.enilu.flash.cache;

import cn.enilu.flash.bean.core.ShiroUser;
import cn.enilu.flash.cache.impl.EhcacheDao;
import cn.enilu.flash.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -13,27 +12,27 @@
public class TokenCache {

@Autowired
private EhcacheDao ehcacheDao;
private CacheDao cacheDao;

public void put(String token, Long idUser) {
ehcacheDao.hset(EhcacheDao.SESSION,token, idUser);
cacheDao.hset(CacheDao.SESSION,token, idUser);
}

public Long get(String token) {
return ehcacheDao.hget(EhcacheDao.SESSION,token,Long.class);
return cacheDao.hget(CacheDao.SESSION,token,Long.class);
}
public Long getIdUser(){
return ehcacheDao.hget(EhcacheDao.SESSION, HttpUtil.getToken(),Long.class );
return cacheDao.hget(CacheDao.SESSION, HttpUtil.getToken(),Long.class );
}

public void remove(String token) {
ehcacheDao.hdel(EhcacheDao.SESSION,token+"user");
cacheDao.hdel(CacheDao.SESSION,token+"user");
}

public void setUser(String token, ShiroUser shiroUser){
ehcacheDao.hset(EhcacheDao.SESSION,token+"user",shiroUser);
cacheDao.hset(CacheDao.SESSION,token+"user",shiroUser);
}
public ShiroUser getUser(String token){
return ehcacheDao.hget(EhcacheDao.SESSION,token+"user",ShiroUser.class);
return cacheDao.hget(CacheDao.SESSION,token+"user",ShiroUser.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ConfigCacheImpl implements ConfigCache {

@Override
public Object get(String key) {
return (String) cacheDao.hget(EhcacheDao.CONSTANT,key);
return (String) cacheDao.hget(CacheDao.CONSTANT,key);
}

@Override
Expand Down Expand Up @@ -61,12 +61,12 @@ public String get(ConfigKeyEnum configKeyEnum) {

@Override
public void set(String key, Object val) {
cacheDao.hset(EhcacheDao.CONSTANT,key,val);
cacheDao.hset(CacheDao.CONSTANT,key,val);
}

@Override
public void del(String key, String val) {
cacheDao.hdel(EhcacheDao.CONSTANT,val);
cacheDao.hdel(CacheDao.CONSTANT,val);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class DictCacheImpl implements DictCache {

@Override
public List<Dict> getDictsByPname(String dictName) {
return (List<Dict>) cacheDao.hget(EhcacheDao.CONSTANT,CacheKey.DICT+dictName,List.class);
return (List<Dict>) cacheDao.hget(CacheDao.CONSTANT,CacheKey.DICT+dictName,List.class);
}

@Override
Expand All @@ -44,7 +44,7 @@ public void cache() {
set(String.valueOf(dict.getId()), children);
set(dict.getName(), children);
for(Dict child:children){
set(CacheKey.DICT_NAME+String.valueOf(child.getId()),child.getName());
set(CacheKey.DICT_NAME+child.getId(),child.getName());
}

}
Expand All @@ -53,12 +53,12 @@ public void cache() {

@Override
public Object get(String key) {
return cacheDao.hget(EhcacheDao.CONSTANT,CacheKey.DICT+key);
return cacheDao.hget(CacheDao.CONSTANT,CacheKey.DICT+key);
}

@Override
public void set(String key, Object val) {
cacheDao.hset(EhcacheDao.CONSTANT,CacheKey.DICT+key,val);
cacheDao.hset(CacheDao.CONSTANT,CacheKey.DICT+key,val);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
import java.io.Serializable;

/**
* EhcacheDao
* Ehcache缓存实现类<br>
* 请不要直接使用该类,而是使用其接口CacheDao,以方便实际应用中往其他缓存服务切换(比如redis,ssdb等)
*
* @author enilu
* @version 2018/9/12 0012
*/
@Component
public class EhcacheDao implements CacheDao {
//缓存常量,永不过期
public static final String CONSTANT = "CONSTANT";
public static final String SESSION = "SESSION";
@Resource
private CacheManager cacheManager;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.enilu.flash.service.system;

import cn.enilu.flash.bean.entity.system.User;
import cn.enilu.flash.cache.impl.EhcacheDao;
import cn.enilu.flash.cache.CacheDao;
import cn.enilu.flash.dao.system.UserRepository;
import cn.enilu.flash.security.JwtUtil;
import cn.enilu.flash.service.BaseService;
Expand All @@ -24,7 +24,7 @@ public class UserService extends BaseService<User,Long,UserRepository> {
@Autowired
private UserRepository userRepository;
@Autowired
private EhcacheDao ehcacheDao;
private CacheDao cacheDao;
@Value("${jwt.token.expire.time}")
private Long tokenExpireTime ;

Expand All @@ -33,19 +33,19 @@ public User findByAccount(String account) {
//由于:@Cacheable标注的方法,如果其所在的类实现了某一个接口,那么该方法也必须出现在接口里面,否则cache无效。
//具体的原因是, Spring把实现类装载成为Bean的时候,会用代理包装一下,所以从Spring Bean的角度看,只有接口里面的方法是可见的,其它的都隐藏了,自然课看不到实现类里面的非接口方法,@Cacheable不起作用。
//所以这里手动控制缓存
User user = ehcacheDao.hget(EhcacheDao.SESSION,account,User.class);
User user = cacheDao.hget(CacheDao.SESSION,account,User.class);
if(user!=null){
return user;
}
user = userRepository.findByAccount(account);
ehcacheDao.hset(EhcacheDao.SESSION,account,user);
cacheDao.hset(CacheDao.SESSION,account,user);
return user;
}

@Override
public User update(User record) {
User user = super.update(record);
ehcacheDao.hset(EhcacheDao.SESSION,user.getAccount(),user);
cacheDao.hset(CacheDao.SESSION,user.getAccount(),user);
return user;
}

Expand All @@ -60,7 +60,7 @@ public String loginForToken(User user){
//将token作为RefreshToken Key 存到缓存中,缓存时间为token有效期的两倍
String refreshTokenCacheKey = token;
Date expireDate = new Date(System.currentTimeMillis()+tokenExpireTime*120000);
ehcacheDao.hset(EhcacheDao.SESSION,refreshTokenCacheKey,String.valueOf(expireDate.getTime()));
cacheDao.hset(CacheDao.SESSION,refreshTokenCacheKey,String.valueOf(expireDate.getTime()));
logger.info("token:{}",token);
return token;
}
Expand All @@ -71,7 +71,7 @@ public String loginForToken(User user){
* @return
*/
public boolean refreshTokenIsValid(String token){
String refreshTokenTime = (String) ehcacheDao.hget(EhcacheDao.SESSION,token);
String refreshTokenTime = (String) cacheDao.hget(CacheDao.SESSION,token);
if(refreshTokenTime == null){
return false;
}
Expand Down

0 comments on commit 4070a18

Please sign in to comment.