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;
}
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);
}
}
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);
}
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);
}
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;
}
Aggregations