Skip to content

Commit 4070a18

Browse files
committed
Change 缓存使用用EhcacheDao而是使用其接口
1 parent 698fd54 commit 4070a18

File tree

8 files changed

+37
-38
lines changed

8 files changed

+37
-38
lines changed

docs/.vuepress/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = {
1515
editLinks: true,
1616
editLinkText: '编辑此页面!',
1717
nav: [
18-
{text: '指南', link: '/'},
18+
{text: '指南', link: '/base/preface'},
1919
{text: '功能',
2020
items:[
2121
{text: '国际化',link:'/action/i18n'},
@@ -76,6 +76,7 @@ module.exports = {
7676
'/feature/exportExcel',
7777
'/feature/messageCenter',
7878
'/feature/cms.md'
79+
7980
]
8081
},
8182
{

flash-api/src/main/resources/ehcache.xml

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
xsi:noNamespaceSchemaLocation="ehcache.xsd"
44
updateCheck="false" monitoring="autodetect"
55
dynamicConfig="true" >
6-
6+
77
<diskStore path="java.io.tmpdir/ehcache"/>
88

99
<defaultCache
@@ -32,8 +32,8 @@
3232
<!--SESSION缓存-->
3333
<cache name="SESSION"
3434
maxElementsInMemory="50000"
35-
timeToIdleSeconds="3600"
36-
timeToLiveSeconds="3600"
35+
timeToIdleSeconds="86400"
36+
timeToLiveSeconds="86400"
3737
eternal="true"
3838
clearOnFlush="false"
3939
overflowToDisk="true"
@@ -65,21 +65,21 @@
6565

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

flash-core/src/main/java/cn/enilu/flash/cache/CacheDao.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import java.io.Serializable;
44

55
/**
6-
* CacheDao
6+
* 缓存接口
77
*
88
* @author enilu
99
* @version 2018/9/12 0012
1010
*/
1111
public interface CacheDao {
12-
12+
String CONSTANT = "CONSTANT";
13+
String SESSION = "SESSION";
1314

1415
/**
1516
* 设置hash key值
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.enilu.flash.cache;
22

33
import cn.enilu.flash.bean.core.ShiroUser;
4-
import cn.enilu.flash.cache.impl.EhcacheDao;
54
import cn.enilu.flash.utils.HttpUtil;
65
import org.springframework.beans.factory.annotation.Autowired;
76
import org.springframework.stereotype.Service;
@@ -13,27 +12,27 @@
1312
public class TokenCache {
1413

1514
@Autowired
16-
private EhcacheDao ehcacheDao;
15+
private CacheDao cacheDao;
1716

1817
public void put(String token, Long idUser) {
19-
ehcacheDao.hset(EhcacheDao.SESSION,token, idUser);
18+
cacheDao.hset(CacheDao.SESSION,token, idUser);
2019
}
2120

2221
public Long get(String token) {
23-
return ehcacheDao.hget(EhcacheDao.SESSION,token,Long.class);
22+
return cacheDao.hget(CacheDao.SESSION,token,Long.class);
2423
}
2524
public Long getIdUser(){
26-
return ehcacheDao.hget(EhcacheDao.SESSION, HttpUtil.getToken(),Long.class );
25+
return cacheDao.hget(CacheDao.SESSION, HttpUtil.getToken(),Long.class );
2726
}
2827

2928
public void remove(String token) {
30-
ehcacheDao.hdel(EhcacheDao.SESSION,token+"user");
29+
cacheDao.hdel(CacheDao.SESSION,token+"user");
3130
}
3231

3332
public void setUser(String token, ShiroUser shiroUser){
34-
ehcacheDao.hset(EhcacheDao.SESSION,token+"user",shiroUser);
33+
cacheDao.hset(CacheDao.SESSION,token+"user",shiroUser);
3534
}
3635
public ShiroUser getUser(String token){
37-
return ehcacheDao.hget(EhcacheDao.SESSION,token+"user",ShiroUser.class);
36+
return cacheDao.hget(CacheDao.SESSION,token+"user",ShiroUser.class);
3837
}
3938
}

flash-core/src/main/java/cn/enilu/flash/cache/impl/ConfigCacheImpl.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ConfigCacheImpl implements ConfigCache {
2929

3030
@Override
3131
public Object get(String key) {
32-
return (String) cacheDao.hget(EhcacheDao.CONSTANT,key);
32+
return (String) cacheDao.hget(CacheDao.CONSTANT,key);
3333
}
3434

3535
@Override
@@ -61,12 +61,12 @@ public String get(ConfigKeyEnum configKeyEnum) {
6161

6262
@Override
6363
public void set(String key, Object val) {
64-
cacheDao.hset(EhcacheDao.CONSTANT,key,val);
64+
cacheDao.hset(CacheDao.CONSTANT,key,val);
6565
}
6666

6767
@Override
6868
public void del(String key, String val) {
69-
cacheDao.hdel(EhcacheDao.CONSTANT,val);
69+
cacheDao.hdel(CacheDao.CONSTANT,val);
7070
}
7171

7272
@Override

flash-core/src/main/java/cn/enilu/flash/cache/impl/DictCacheImpl.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class DictCacheImpl implements DictCache {
2525

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

3131
@Override
@@ -44,7 +44,7 @@ public void cache() {
4444
set(String.valueOf(dict.getId()), children);
4545
set(dict.getName(), children);
4646
for(Dict child:children){
47-
set(CacheKey.DICT_NAME+String.valueOf(child.getId()),child.getName());
47+
set(CacheKey.DICT_NAME+child.getId(),child.getName());
4848
}
4949

5050
}
@@ -53,12 +53,12 @@ public void cache() {
5353

5454
@Override
5555
public Object get(String key) {
56-
return cacheDao.hget(EhcacheDao.CONSTANT,CacheKey.DICT+key);
56+
return cacheDao.hget(CacheDao.CONSTANT,CacheKey.DICT+key);
5757
}
5858

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

6363
}
6464
}

flash-core/src/main/java/cn/enilu/flash/cache/impl/EhcacheDao.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@
99
import java.io.Serializable;
1010

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

flash-core/src/main/java/cn/enilu/flash/service/system/UserService.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cn.enilu.flash.service.system;
22

33
import cn.enilu.flash.bean.entity.system.User;
4-
import cn.enilu.flash.cache.impl.EhcacheDao;
4+
import cn.enilu.flash.cache.CacheDao;
55
import cn.enilu.flash.dao.system.UserRepository;
66
import cn.enilu.flash.security.JwtUtil;
77
import cn.enilu.flash.service.BaseService;
@@ -24,7 +24,7 @@ public class UserService extends BaseService<User,Long,UserRepository> {
2424
@Autowired
2525
private UserRepository userRepository;
2626
@Autowired
27-
private EhcacheDao ehcacheDao;
27+
private CacheDao cacheDao;
2828
@Value("${jwt.token.expire.time}")
2929
private Long tokenExpireTime ;
3030

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

4545
@Override
4646
public User update(User record) {
4747
User user = super.update(record);
48-
ehcacheDao.hset(EhcacheDao.SESSION,user.getAccount(),user);
48+
cacheDao.hset(CacheDao.SESSION,user.getAccount(),user);
4949
return user;
5050
}
5151

@@ -60,7 +60,7 @@ public String loginForToken(User user){
6060
//将token作为RefreshToken Key 存到缓存中,缓存时间为token有效期的两倍
6161
String refreshTokenCacheKey = token;
6262
Date expireDate = new Date(System.currentTimeMillis()+tokenExpireTime*120000);
63-
ehcacheDao.hset(EhcacheDao.SESSION,refreshTokenCacheKey,String.valueOf(expireDate.getTime()));
63+
cacheDao.hset(CacheDao.SESSION,refreshTokenCacheKey,String.valueOf(expireDate.getTime()));
6464
logger.info("token:{}",token);
6565
return token;
6666
}
@@ -71,7 +71,7 @@ public String loginForToken(User user){
7171
* @return
7272
*/
7373
public boolean refreshTokenIsValid(String token){
74-
String refreshTokenTime = (String) ehcacheDao.hget(EhcacheDao.SESSION,token);
74+
String refreshTokenTime = (String) cacheDao.hget(CacheDao.SESSION,token);
7575
if(refreshTokenTime == null){
7676
return false;
7777
}

0 commit comments

Comments
 (0)