本地缓存GavaCache使用

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>19.0</version>
</dependency>


private static Cache<Long, ABTest> abTestCache = CacheBuilder.newBuilder()
        .initialCapacity(1)
        .maximumSize(20)
        .expireAfterAccess(1, TimeUnit.DAYS)
        .build();

@Override
public ABTest getABTestByUnitId(Long unitId) {
    return getABTest(unitId);
}

/**
* Select from cache, If cache missed , load from DB .
* @param unitId
* @return
*/
public ABTest getABTest(Long unitId){
    try {
        ABTest abTest = abTestCache.get(unitId, new Callable<ABTest>() {
            @Override
            public ABTest call() throws Exception {
                if(!isStoredInDB(unitId)){
                    return new ABTest(unitId,BASE_VERSION);
                }else {
                    return loadFromDB(unitId);
                }
            }
        });
        return abTest;
    } catch (ExecutionException e) {
        log.error("ABTestDomainCache load abtest error!");
    }
    return new ABTest(unitId,BASE_VERSION);
}

@Override
public void invalidate(Long unitId){
    abTestCache.invalidate(unitId);
}

/**
* Check whether this unitId have saved param in DB .
* @param unitId
* @return
*/
public boolean isStoredInDB(Long unitId){
    List<Bucket> buckets = bucketService.getByUnitId(unitId);
    List<Selector> selectors = selectorService.getByUnitId(unitId);
    if(CollectionUtils.isEmpty(buckets)&&CollectionUtils.isEmpty(selectors)){
        return false;
    }
    return true;
}

public ABTest loadFromDB(Long unitId){
    ABTest abTest = new ABTest(unitId,BASE_VERSION);
    BucketParam bucketParam = getBucketParams(unitId);
    SelectorParam selectorParam = getSelectorParam(unitId);

    Unit unit = unitService.getById(unitId);
    abTest.initActionAndSelector(unit.getActionType(),bucketParam,selectorParam);
    abTest.initTableTemplate();
    abTest.initTableBody(bucketParam,selectorParam);
    return abTest;
}

 

接口:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ximalaya.football.privilege.privilege.cache;

public interface PrivilegeCache {
    void addPrivilege(String var1, PrivilegeInfo var2);

    PrivilegeInfo getPrivilege(String var1);

    void remove(String var1);

    void removeAll();
}

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ximalaya.football.privilege.privilege.cache;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.util.concurrent.TimeUnit;

public class LocalPrivilegeCacheWIthExpire implements PrivilegeCache {
    private static final int CACHE_EXPIRE_SECOND = 60;
    private static final Cache<String, PrivilegeInfo> cache;

    public LocalPrivilegeCacheWIthExpire() {
    }

    public void addPrivilege(String key, PrivilegeInfo value) {
        cache.put(key, value);
    }

    public PrivilegeInfo getPrivilege(String key) {
        return (PrivilegeInfo)cache.getIfPresent(key);
    }

    public void remove(String key) {
        if (key != null && key != "null") {
            cache.invalidate(key);
        }
    }

    public void removeAll() {
        cache.invalidateAll();
    }

    static {
        cache = CacheBuilder.newBuilder().initialCapacity(256).maximumSize(512L).expireAfterWrite(60L, TimeUnit.SECONDS).build();
    }
}


使用示例:
public class PrivilegeFacade {
    private static SystemRoleAPI systemRoleAPI = (SystemRoleAPI)AuthSpringContextHolder.getBean(SystemRoleAPI.class);
    private static AppRoleAPI appRoleAPI = (AppRoleAPI)AuthSpringContextHolder.getBean(AppRoleAPI.class);
    private static GroupRoleAPI groupRoleAPI = (GroupRoleAPI)AuthSpringContextHolder.getBean(GroupRoleAPI.class);
    private static final Logger LOGGER = LoggerFactory.getLogger(PrivilegeFacade.class);
    private static final PrivilegeCache privilegeCache = new LocalPrivilegeCacheWIthExpire();

    public PrivilegeFacade() {
    }

    private static PrivilegeInfo findPrivilegesByUserName(String userName) {
        PrivilegeInfo privilegeInfo = privilegeCache.getPrivilege(userName);
        if (privilegeInfo == null) {
            privilegeInfo = new PrivilegeInfo();
            List<String> privilegeList = new ArrayList();
            privilegeInfo.setPrivilegeList(privilegeList);
            List<UserSystemRoleDTO> userSystemRoleList = systemRoleAPI.findUserSystemRoleList(userName);
            if (CollectionUtils.isNotEmpty(userSystemRoleList)) {
                Iterator var4 = userSystemRoleList.iterator();

                while(var4.hasNext()) {
                    UserSystemRoleDTO userSystemRole = (UserSystemRoleDTO)var4.next();
                    privilegeList.addAll(PrivilegeUtils.formatSystemRolePrivileges(userSystemRole.getRoleType()));
                }
            }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值