本地缓存GavaCache使用


    com.google.guava
    guava
    19.0


private static Cache 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() {
            @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 buckets = bucketService.getByUnitId(unitId);
    List 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 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 privilegeList = new ArrayList();
            privilegeInfo.setPrivilegeList(privilegeList);
            List 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()));
                }
            }

 

 

你可能感兴趣的:(javaweb)