Search in sources :

Example 1 with CacheKey

use of com.albedo.java.common.core.cache.model.CacheKey in project albedo by somowhere.

the class AbstractCacheServiceImpl method findByIds.

@Override
@Transactional(readOnly = true)
public List<T> findByIds(@NonNull Collection<? extends Serializable> ids, Function<Collection<? extends Serializable>, Collection<T>> loader) {
    if (ids.isEmpty()) {
        return Collections.emptyList();
    }
    // 拼接keys
    List<CacheKey> keys = ids.stream().map(cacheKeyBuilder()::key).collect(Collectors.toList());
    // 切割
    List<List<CacheKey>> partitionKeys = Lists.partition(keys, MAX_BATCH_KEY_SIZE);
    // 用切割后的 partitionKeys 分批去缓存查, 返回的是缓存中存在的数据
    List<T> valueList = partitionKeys.stream().map(ks -> (List<T>) cacheOps.find(ks)).flatMap(Collection::stream).collect(Collectors.toList());
    // 所有的key
    List<Serializable> keysList = Lists.newArrayList(ids);
    // 缓存不存在的key
    Set<Serializable> missedKeys = Sets.newLinkedHashSet();
    List<T> allList = new ArrayList<>();
    for (int i = 0; i < valueList.size(); i++) {
        T v = valueList.get(i);
        Serializable k = keysList.get(i);
        if (v == null) {
            missedKeys.add(k);
        } else {
            allList.add(v);
        }
    }
    // 加载miss 的数据,并设置到缓存
    if (CollUtil.isNotEmpty(missedKeys)) {
        if (loader == null) {
            loader = this::listByIds;
        }
        Collection<T> missList = loader.apply(missedKeys);
        missList.forEach(this::setCache);
        allList.addAll(missList);
    }
    return allList;
}
Also used : Serializable(java.io.Serializable) CacheKey(com.albedo.java.common.core.cache.model.CacheKey) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with CacheKey

use of com.albedo.java.common.core.cache.model.CacheKey in project albedo by somowhere.

the class AbstractCacheServiceImpl method delCache.

protected void delCache(T model) {
    Object id = getId(model);
    if (id != null) {
        CacheKey key = cacheKeyBuilder().key(id);
        cacheOps.del(key);
    }
}
Also used : CacheKey(com.albedo.java.common.core.cache.model.CacheKey)

Example 3 with CacheKey

use of com.albedo.java.common.core.cache.model.CacheKey in project albedo by somowhere.

the class ApplicationServiceImpl method getByClient.

@Override
public ApplicationDo getByClient(String clientId, String clientSecret) {
    LbqWrapper<ApplicationDo> wrapper = Wraps.<ApplicationDo>lbQ().select(ApplicationDo::getId).eq(ApplicationDo::getClientId, clientId).eq(ApplicationDo::getClientSecret, clientSecret);
    Function<CacheKey, Object> loader = k -> super.getObj(wrapper, Convert::toLong);
    CacheKey cacheKey = new ApplicationClientCacheKeyBuilder().key(clientId, clientSecret);
    return getByKey(cacheKey, loader);
}
Also used : Wraps(com.albedo.java.plugins.database.mybatis.conditions.Wraps) ApplicationClientCacheKeyBuilder(com.albedo.java.modules.sys.cache.ApplicationClientCacheKeyBuilder) ApplicationDo(com.albedo.java.modules.sys.domain.ApplicationDo) LbqWrapper(com.albedo.java.plugins.database.mybatis.conditions.query.LbqWrapper) ApplicationDto(com.albedo.java.modules.sys.domain.dto.ApplicationDto) CacheKeyBuilder(com.albedo.java.common.core.cache.model.CacheKeyBuilder) Function(java.util.function.Function) ApplicationService(com.albedo.java.modules.sys.service.ApplicationService) ApplicationRepository(com.albedo.java.modules.sys.repository.ApplicationRepository) Service(org.springframework.stereotype.Service) AbstractDataCacheServiceImpl(com.albedo.java.plugins.database.mybatis.service.impl.AbstractDataCacheServiceImpl) Convert(cn.hutool.core.convert.Convert) CacheKey(com.albedo.java.common.core.cache.model.CacheKey) Convert(cn.hutool.core.convert.Convert) ApplicationDo(com.albedo.java.modules.sys.domain.ApplicationDo) ApplicationClientCacheKeyBuilder(com.albedo.java.modules.sys.cache.ApplicationClientCacheKeyBuilder) CacheKey(com.albedo.java.common.core.cache.model.CacheKey)

Example 4 with CacheKey

use of com.albedo.java.common.core.cache.model.CacheKey in project albedo by somowhere.

the class LogLoginServiceImpl method pvIncr.

@Override
public void pvIncr() {
    CacheKey totalPvKey = TotalPvCacheKeyBuilder.build();
    cacheOps.incr(totalPvKey);
    CacheKey todayPvKey = TodayPvCacheKeyBuilder.build(LocalDate.now());
    cacheOps.incr(todayPvKey);
}
Also used : CacheKey(com.albedo.java.common.core.cache.model.CacheKey)

Example 5 with CacheKey

use of com.albedo.java.common.core.cache.model.CacheKey in project albedo by somowhere.

the class ParameterServiceImpl method save.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean save(ParameterDo model) {
    ArgumentAssert.isFalse(check(model.getKey()), "参数key重复");
    boolean bool = SqlHelper.retBool(baseMapper.insert(model));
    if (bool) {
        CacheKey cacheKey = new ParameterKeyCacheKeyBuilder().key(model.getKey());
        cacheOps.set(cacheKey, model.getValue());
    }
    return bool;
}
Also used : ParameterKeyCacheKeyBuilder(com.albedo.java.modules.sys.cache.ParameterKeyCacheKeyBuilder) CacheKey(com.albedo.java.common.core.cache.model.CacheKey) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CacheKey (com.albedo.java.common.core.cache.model.CacheKey)17 Transactional (org.springframework.transaction.annotation.Transactional)12 Service (org.springframework.stereotype.Service)8 RequiredArgsConstructor (lombok.RequiredArgsConstructor)6 Convert (cn.hutool.core.convert.Convert)5 StrUtil (cn.hutool.core.util.StrUtil)5 CacheOps (com.albedo.java.plugins.cache.repository.CacheOps)5 List (java.util.List)5 ArgumentAssert (com.albedo.java.common.core.util.ArgumentAssert)4 ParameterKeyCacheKeyBuilder (com.albedo.java.modules.sys.cache.ParameterKeyCacheKeyBuilder)4 UserVo (com.albedo.java.modules.sys.domain.vo.UserVo)4 Wraps (com.albedo.java.plugins.database.mybatis.conditions.Wraps)4 LocalDate (java.time.LocalDate)4 Function (java.util.function.Function)4 Collectors (java.util.stream.Collectors)4 DatePattern (cn.hutool.core.date.DatePattern)3 LocalDateTimeUtil (cn.hutool.core.date.LocalDateTimeUtil)3 CacheKeyBuilder (com.albedo.java.common.core.cache.model.CacheKeyBuilder)3 MapHelper (com.albedo.java.common.core.util.MapHelper)3 ParameterUpdateEvent (com.albedo.java.common.event.listener.ParameterUpdateEvent)3