net.oschina.j2cache.CacheObject Java Examples

The following examples show how to use net.oschina.j2cache.CacheObject. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SSOAuthentication.java    From Aooms with Apache License 2.0 6 votes vote down vote up
public static boolean refreshAuthenticationInfo(AuthenticationInfo authenticationInfo){
    if(authenticationInfo == null) return true;
    HttpServletRequest request = AoomsContext.getRequest();
    SSOToken ssoToken = (SSOToken) request.getAttribute(SSOConstants.SSO_TOKEN_ATTR);
    if(ssoToken == null){
        return false;
    }

    String cacheGroup = ssoToken.getClaims().get(CACHE_GROUP_PLACEHOLDER,String.class);
    long cacheTimeout = ssoToken.getClaims().get(CACHE_TIMEOUT_PLACEHOLDER,Integer.class);
    if(cacheGroup == null) return false;

    CacheObject cacheObject = Aooms.self().getJ2Cache().get(cacheGroup,authenticationInfo.getSessionId());
    // 缓存对象不存在
    if(cacheObject == null){
        return false;
    }

    cacheAuthenticationInfo(cacheGroup, cacheTimeout ,authenticationInfo);
    return true;
}
 
Example #2
Source File: RoleServiceImpl.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
@Override
public List<Role> findRoleByUserId(Long userId) {
    String key = key(userId);
    List<Role> roleList = new ArrayList<>();
    CacheObject cacheObject = cacheChannel.get(CacheKey.USER_ROLE, key, (k) -> {
        roleList.addAll(baseMapper.findRoleByUserId(userId));
        return roleList.stream().mapToLong(Role::getId).boxed().collect(Collectors.toList());
    });

    if (cacheObject.getValue() == null) {
        return Collections.emptyList();
    }


    if (!roleList.isEmpty()) {
        // TODO 异步性能 更加
        roleList.forEach((item) -> {
            String itemKey = key(item.getId());
            cacheChannel.set(ROLE, itemKey, item);
        });

        return roleList;
    }

    List<Long> list = (List<Long>) cacheObject.getValue();

    List<Role> menuList = list.stream().map(this::getByIdCache)
            .filter(Objects::nonNull).collect(Collectors.toList());

    return menuList;
}
 
Example #3
Source File: J2CacheGeneralDataRegion.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(SharedSessionContractImplementor session, Object key) throws CacheException {
    LOG.debugf("key: %s", key);
    if (key == null) {
        return null;
    } else {
        CacheObject value = this.getCache().get(key);
        if (value == null) {
            LOG.debugf("value for key %s is null", key);
            return null;
        } else {
            return value.getValue();
        }
    }
}
 
Example #4
Source File: J2HibernateCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(Object key) throws CacheException {
    CacheObject cobj = cache.get(region, key.toString());
    if (log.isDebugEnabled())
        log.debug("get value for j2cache which key:" + key + ",value:" + cobj.getValue());
    return cobj.getValue();
}
 
Example #5
Source File: ValidateCodeServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public R<Boolean> check(String key, String value) {
    if (StringUtils.isBlank(value)) {
        return R.fail(CAPTCHA_ERROR.build("请输入验证码"));
    }
    CacheObject cacheObject = cache.get(CacheKey.CAPTCHA, key);
    if (cacheObject.getValue() == null) {
        return R.fail(CAPTCHA_ERROR.build("验证码已过期"));
    }
    if (!StringUtils.equalsIgnoreCase(value, String.valueOf(cacheObject.getValue()))) {
        return R.fail(CAPTCHA_ERROR.build("验证码不正确"));
    }
    cache.evict(CacheKey.CAPTCHA, key);
    return R.success(true);
}
 
Example #6
Source File: J2CacheGeneralDataRegion.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(Object key) throws CacheException {
    LOG.debugf("key: %s", key);
    if (key == null) {
        return null;
    } else {
        CacheObject value = getCache().get(key);
        if (value == null) {
            LOG.debugf("value for key %s is null", key);
            return null;
        } else {
            return value.getValue();
        }
    }
}
 
Example #7
Source File: MenuServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 查询用户可用菜单
 * 1,查询缓存中存放的当前用户拥有的所有菜单列表 [menuId,menuId...]
 * 2,缓存&DB为空则返回
 * 3,缓存总用户菜单列表 为空,但db存在,则将list便利set到缓存,并直接返回
 * 4,缓存存在用户菜单列表,则根据菜单id遍历去缓存查询菜单。
 * 5,过滤group后,返回
 *
 * <p>
 * 注意:什么地方需要清除 USER_MENU 缓存
 * 给用户重新分配角色时, 角色重新分配资源/菜单时
 *
 * @param group
 * @param userId
 * @return
 */
@Override
public List<Menu> findVisibleMenu(String group, Long userId) {
    String key = key(userId);
    List<Menu> visibleMenu = new ArrayList<>();
    CacheObject cacheObject = cacheChannel.get(CacheKey.USER_MENU, key, (k) -> {
        visibleMenu.addAll(baseMapper.findVisibleMenu(userId));
        return visibleMenu.stream().mapToLong(Menu::getId).boxed().collect(Collectors.toList());
    });

    if (cacheObject.getValue() == null) {
        return Collections.emptyList();
    }

    if (!visibleMenu.isEmpty()) {
        // TODO 异步性能 更加
        visibleMenu.forEach((menu) -> {
            String menuKey = key(menu.getId());
            cacheChannel.set(MENU, menuKey, menu);
        });

        return menuListFilterGroup(group, visibleMenu);
    }

    List<Long> list = (List<Long>) cacheObject.getValue();

    List<Menu> menuList = list.stream().map(this::getByIdCache)
            .filter(Objects::nonNull).collect(Collectors.toList());

    return menuListFilterGroup(group, menuList);
}
 
Example #8
Source File: RoleServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public List<Role> findRoleByUserId(Long userId) {
    String key = key(userId);
    List<Role> roleList = new ArrayList<>();
    CacheObject cacheObject = cacheChannel.get(CacheKey.USER_ROLE, key, (k) -> {
        roleList.addAll(baseMapper.findRoleByUserId(userId));
        return roleList.stream().mapToLong(Role::getId).boxed().collect(Collectors.toList());
    });

    if (cacheObject.getValue() == null) {
        return Collections.emptyList();
    }


    if (!roleList.isEmpty()) {
        // TODO 异步性能 更加
        roleList.forEach((item) -> {
            String itemKey = key(item.getId());
            cacheChannel.set(ROLE, itemKey, item);
        });

        return roleList;
    }

    List<Long> list = (List<Long>) cacheObject.getValue();

    List<Role> menuList = list.stream().map(this::getByIdCache)
            .filter(Objects::nonNull).collect(Collectors.toList());

    return menuList;
}
 
Example #9
Source File: ResourceServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 查询用户的可用资源
 *
 * 注意:什么地方需要清除 USER_MENU 缓存
 * 给用户重新分配角色时, 角色重新分配资源/菜单时
 *
 * @param resource
 * @return
 */
@Override
public List<Resource> findVisibleResource(ResourceQueryDTO resource) {
    //1, 先查 cache,cache中没有就执行回调查询DB,并设置到缓存
    String userResourceKey = key(resource.getUserId());

    List<Resource> visibleResource = new ArrayList<>();
    CacheObject cacheObject = cacheChannel.get(CacheKey.USER_RESOURCE, userResourceKey, (key) -> {
        visibleResource.addAll(baseMapper.findVisibleResource(resource));
        return visibleResource.stream().mapToLong(Resource::getId).boxed().collect(Collectors.toList());
    });

    //cache 和 db 都没有时直接返回
    if (cacheObject.getValue() == null) {
        return Collections.emptyList();
    }

    if (!visibleResource.isEmpty()) {
        visibleResource.forEach((r) -> {
            String menuKey = key(r.getId());
            cacheChannel.set(RESOURCE, menuKey, r);
        });
        return resourceListFilterGroup(resource.getMenuId(), visibleResource);
    }

    // 若list里面的值过多,而资源又均没有缓存(或者缓存击穿),则这里的效率并不高

    List<Long> list = (List<Long>) cacheObject.getValue();
    List<Resource> resourceList = list.stream().map(this::getByIdCache).filter(Objects::nonNull).collect(Collectors.toList());

    if (resource.getMenuId() == null) {
        return resourceList;
    }

    // 根据查询条件过滤数据
    return resourceListFilterGroup(resource.getMenuId(), visibleResource);
}
 
Example #10
Source File: J2CacheCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
protected Object lookup(Object key) {
	CacheObject cacheObject = cacheChannel.get(j2CacheName, String.valueOf(key), false);
	if(cacheObject.rawValue() != null && cacheObject.rawValue().getClass().equals(NullObject.class) && super.isAllowNullValues()) {
		return NullValue.INSTANCE;
	}
	return cacheObject.getValue();
}
 
Example #11
Source File: UserTokenServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 标记上一个token为被T状态
 *
 * @param user
 */
private void evictPreviousToken(CacheObject user) {
    if (user.getValue() != null) {
        String previousToken = (String) user.getValue();
        channel.set(CacheKey.TOKEN_USER_ID, CacheKey.buildKey(previousToken), BizConstant.LOGIN_STATUS);
        super.remove(Wraps.<UserToken>lbQ().eq(UserToken::getToken, previousToken));
    }
}
 
Example #12
Source File: J2CacheCache.java    From J2Cache with Apache License 2.0 5 votes vote down vote up
@Override
protected Object lookup(Object key) {
	CacheObject cacheObject = cacheChannel.get(j2CacheName, String.valueOf(key), false);
	if(cacheObject.rawValue() != null && cacheObject.rawValue().getClass().equals(NullObject.class) && super.isAllowNullValues()) {
		return NullValue.INSTANCE;
	}
	return cacheObject.getValue();
}
 
Example #13
Source File: ExampleController.java    From Aooms with Apache License 2.0 5 votes vote down vote up
/**
 * 缓存操作
 */
@RequestMapping("/example4")
public void example4(){
    cacheChannel.set("testRegion","id","root");
    CacheObject cacheObject = cacheChannel.get("testRegion","id");
    logger.info("cacheValue = {}" ,cacheObject.asString());

    // 缓存操作二
    // CacheChannel channel = Aooms.self().getJ2Cache();
}
 
Example #14
Source File: J2CacheCache.java    From spring-boot-tutorial with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
@Override
protected Object lookup(Object key) {
    CacheObject cacheObject = cacheChannel.get(j2CacheName, String.valueOf(key));
    if (cacheObject.rawValue() != null && cacheObject.rawValue().getClass().equals(NullObject.class)
        && super.isAllowNullValues()) {
        return NullValue.INSTANCE;
    }
    return cacheObject.getValue();
}
 
Example #15
Source File: ValidateCodeServiceImpl.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
@Override
public R<Boolean> check(String key, String value) {
    if (StringUtils.isBlank(value)) {
        return R.fail(CAPTCHA_ERROR.build("请输入验证码"));
    }
    CacheObject cacheObject = cache.get(CacheKey.CAPTCHA, key);
    if (cacheObject.getValue() == null) {
        return R.fail(CAPTCHA_ERROR.build("验证码已过期"));
    }
    if (!StringUtils.equalsIgnoreCase(value, String.valueOf(cacheObject.getValue()))) {
        return R.fail(CAPTCHA_ERROR.build("验证码不正确"));
    }
    cache.evict(CacheKey.CAPTCHA, key);
    return R.success(true);
}
 
Example #16
Source File: ParameterServiceImpl.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
@Override
public String getValue(String key, String defVal) {
    if (StrUtil.isEmpty(key)) {
        return defVal;
    }

    String cacheKey = buildTenantKey(key);
    CacheObject cacheObject = channel.get(getRegion(), cacheKey, (k) -> {
        Parameter parameter = getOne(Wraps.<Parameter>lbQ().eq(Parameter::getKey, key).eq(Parameter::getStatus, true));
        return parameter == null ? null : parameter.getValue();
    }, true);

    return (String) cacheObject.getValue();
}
 
Example #17
Source File: ResourceServiceImpl.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 查询用户的可用资源
 *
 * 注意:什么地方需要清除 USER_MENU 缓存
 * 给用户重新分配角色时, 角色重新分配资源/菜单时
 *
 * @param resource
 * @return
 */
@Override
public List<Resource> findVisibleResource(ResourceQueryDTO resource) {
    //1, 先查 cache,cache中没有就执行回调查询DB,并设置到缓存
    String userResourceKey = key(resource.getUserId());

    List<Resource> visibleResource = new ArrayList<>();
    CacheObject cacheObject = cacheChannel.get(CacheKey.USER_RESOURCE, userResourceKey, (key) -> {
        visibleResource.addAll(baseMapper.findVisibleResource(resource));
        return visibleResource.stream().mapToLong(Resource::getId).boxed().collect(Collectors.toList());
    });

    //cache 和 db 都没有时直接返回
    if (cacheObject.getValue() == null) {
        return Collections.emptyList();
    }

    if (!visibleResource.isEmpty()) {
        visibleResource.forEach((r) -> {
            String menuKey = key(r.getId());
            cacheChannel.set(RESOURCE, menuKey, r);
        });
        return resourceListFilterGroup(resource.getMenuId(), visibleResource);
    }

    // 若list里面的值过多,而资源又均没有缓存(或者缓存击穿),则这里的效率并不高

    List<Long> list = (List<Long>) cacheObject.getValue();
    List<Resource> resourceList = list.stream().map(this::getByIdCache).filter(Objects::nonNull).collect(Collectors.toList());

    if (resource.getMenuId() == null) {
        return resourceList;
    }

    // 根据查询条件过滤数据
    return resourceListFilterGroup(resource.getMenuId(), visibleResource);
}
 
Example #18
Source File: ParameterServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public String getValue(String key, String defVal) {
    if (StrUtil.isEmpty(key)) {
        return defVal;
    }

    String cacheKey = buildTenantKey(key);
    CacheObject cacheObject = channel.get(getRegion(), cacheKey, (k) -> {
        Parameter parameter = getOne(Wraps.<Parameter>lbQ().eq(Parameter::getKey, key).eq(Parameter::getStatus, true));
        return parameter == null ? null : parameter.getValue();
    }, true);

    return (String) cacheObject.getValue();
}
 
Example #19
Source File: J2Cache.java    From t-io with Apache License 2.0 5 votes vote down vote up
@Override
public Serializable _get(String key) {
	CacheChannel cache = getChannel();
	CacheObject cacheObject = cache.get(cacheName, key);
	if (cacheObject != null) {
		return (Serializable) cacheObject.getValue();
	}
	return null;
}
 
Example #20
Source File: MenuServiceImpl.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 查询用户可用菜单
 * 1,查询缓存中存放的当前用户拥有的所有菜单列表 [menuId,menuId...]
 * 2,缓存&DB为空则返回
 * 3,缓存总用户菜单列表 为空,但db存在,则将list便利set到缓存,并直接返回
 * 4,缓存存在用户菜单列表,则根据菜单id遍历去缓存查询菜单。
 * 5,过滤group后,返回
 *
 * <p>
 * 注意:什么地方需要清除 USER_MENU 缓存
 * 给用户重新分配角色时, 角色重新分配资源/菜单时
 *
 * @param group
 * @param userId
 * @return
 */
@Override
public List<Menu> findVisibleMenu(String group, Long userId) {
    String key = key(userId);
    List<Menu> visibleMenu = new ArrayList<>();
    CacheObject cacheObject = cacheChannel.get(CacheKey.USER_MENU, key, (k) -> {
        visibleMenu.addAll(baseMapper.findVisibleMenu(userId));
        return visibleMenu.stream().mapToLong(Menu::getId).boxed().collect(Collectors.toList());
    });

    if (cacheObject.getValue() == null) {
        return Collections.emptyList();
    }

    if (!visibleMenu.isEmpty()) {
        // TODO 异步性能 更加
        visibleMenu.forEach((menu) -> {
            String menuKey = key(menu.getId());
            cacheChannel.set(MENU, menuKey, menu);
        });

        return menuListFilterGroup(group, visibleMenu);
    }

    List<Long> list = (List<Long>) cacheObject.getValue();

    List<Menu> menuList = list.stream().map(this::getByIdCache)
            .filter(Objects::nonNull).collect(Collectors.toList());

    return menuListFilterGroup(group, menuList);
}
 
Example #21
Source File: UserTokenServiceImpl.java    From zuihou-admin-boot with Apache License 2.0 5 votes vote down vote up
/**
 * 标记上一个token为被T状态
 *
 * @param user
 */
private void evictPreviousToken(CacheObject user) {
    if (user.getValue() != null) {
        String previousToken = (String) user.getValue();
        channel.set(CacheKey.TOKEN_USER_ID, CacheKey.buildKey(previousToken), BizConstant.LOGIN_STATUS);
        super.remove(Wraps.<UserToken>lbQ().eq(UserToken::getToken, previousToken));
    }
}
 
Example #22
Source File: TransactionalJ2CacheCollectionRegionAccessStrategy.java    From J2Cache with Apache License 2.0 4 votes vote down vote up
@Override
public Object get(SharedSessionContractImplementor session, Object key, long txTimestamp) throws CacheException {
    CacheObject object = cache.get(key);
    return object != null ? object.getValue() : null;
}
 
Example #23
Source File: TransactionalJ2CacheNaturalIdRegionAccessStrategy.java    From J2Cache with Apache License 2.0 4 votes vote down vote up
@Override
public Object get(SharedSessionContractImplementor session, Object key, long txTimestamp) throws CacheException {
    final CacheObject element = cache.get(key);
    return element == null ? null : element.getValue();
}
 
Example #24
Source File: J2cacheImpl.java    From jboot with Apache License 2.0 4 votes vote down vote up
@Override
public <T> T get(String cacheName, Object key) {
    CacheObject cacheObject = J2Cache.getChannel().get(cacheName, key.toString(), false);
    return cacheObject != null ? (T) cacheObject.getValue() : null;
}
 
Example #25
Source File: TransactionalJ2CacheEntityRegionAccessStrategy.java    From J2Cache with Apache License 2.0 4 votes vote down vote up
@Override
public Object get(SharedSessionContractImplementor session, Object key, long txTimestamp) throws CacheException {
    final CacheObject element = cache.get(key);
    return element == null ? null : element.getValue();
}
 
Example #26
Source File: LettuceCacheProvider.java    From J2Cache with Apache License 2.0 4 votes vote down vote up
@Override
public int level() {
    return CacheObject.LEVEL_2;
}
 
Example #27
Source File: EhCacheProvider3.java    From J2Cache with Apache License 2.0 4 votes vote down vote up
@Override
public int level() {
    return CacheObject.LEVEL_1;
}
 
Example #28
Source File: EhCacheProvider.java    From J2Cache with Apache License 2.0 4 votes vote down vote up
@Override
public int level() {
    return CacheObject.LEVEL_1;
}
 
Example #29
Source File: J2CacheController.java    From zuihou-admin-cloud with Apache License 2.0 4 votes vote down vote up
@ApiOperation(value = "数据读取", notes = "数据读取")
@GetMapping("/get")
public R<Object> get(String region, String key) {
    CacheObject cacheObject = cache.get(region, key);
    return R.success(String.format("%s:%s====%s", region, key, cacheObject));
}
 
Example #30
Source File: LoginLogServiceImpl.java    From zuihou-admin-cloud with Apache License 2.0 4 votes vote down vote up
@Override
public List<Map<String, Object>> findByOperatingSystem() {
    CacheObject cacheObject = this.cache.get(CacheKey.LOGIN_LOG_SYSTEM, CacheKey.buildTenantKey(), (key) -> this.baseMapper.findByOperatingSystem());
    return (List<Map<String, Object>>) cacheObject.getValue();
}