两个接口抽象 Cache,CacheManager,具体的实现都是基于这两个抽象实现。 典型的SPI机制,和eat your dog food。当需要提供接口给外部调用,首先自己内部的实现也必须基于同样一套抽象机制
The cache abstraction does not provide an actual store and relies on abstraction materialized by the org.springframework.cache.Cache and org.springframework.cache.CacheManager interfaces.
Spring Cache提供了这些缓存的实现,如果没有一种CacheManage,或者CacheResolver,会按照指定的顺序去实现
If you have not defined a bean of type CacheManager or a CacheResolver named cacheResolver (see CachingConfigurer), Spring Boot tries to detect the following providers (in the indicated order): 1.Generic 2.JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others) 3.EhCache 2.x 4.Hazelcast 5.Infinispan 6.Couchbase 7.Redis 8.Caffeine 9.Simple
step2 run demo
对Spring Cache有了一个大概的了解后,我们首先使用起来,跑个demo。
定义一个用户查询方法
@Component
public class CacheSample {
@Cacheable(cacheNames = "users")
public Map getUser(final Collection userIds) {
System.out.println("not cache");
final Map mapUser = new HashMap<>();
userIds.forEach(userId -> {
mapUser.put(userId, User.builder().userId(userId).name("name").build());
});
return mapUser;
}
配置一个CacheManager
@Configuration
public class CacheConfig {
@Primary
@Bean(name = { "cacheManager" })
public CacheManager getCache() {
return new ConcurrentMapCacheManager("users");
}
API调用
@RestController
@RequestMapping("/api/cache")
public class CacheController {
@Autowired
private CacheSample cacheSample;
@GetMapping("/user/v1/1")
public List getUser() {
return cacheSample.getUser(Arrays.asList(1L,2L)).values().stream().collect(Collectors.toList());
}
}
// Check if we have a cached item matching the conditions
Cache.ValueWrapper cacheHit = findCachedItem(contexts.get(CacheableOperation.class));
// Collect puts from any @Cacheable miss, if no cached item is found
List cachePutRequests = new LinkedList<>();
if (cacheHit == null) {
collectPutRequests(contexts.get(CacheableOperation.class),
CacheOperationExpressionEvaluator.NO_RESULT, cachePutRequests);
}
public static Object lookup(final CacheExtension cache, final Object key) {
if (key instanceof Collection) {
final Collection originalKeys = ((Collection) key);
if (originalKeys == null || originalKeys.isEmpty()) {
return CacheResult.builder().cache(cache).miss(
Collections.emptySet())
.build();
}
final List keys = originalKeys.stream()
.filter(Objects::nonNull).collect(Collectors.toList());
final Map hits = cache.getAll(keys);
final Set miss = new HashSet(keys);
miss.removeAll(hits.keySet());
return CacheResult.builder().cache(cache).hit(hits).miss(miss).build();
}
return null;
}
CacheResult就是新的缓存结果格式
@Builder
@Setter
@Getter
static class CacheResult {
final CacheExtension cache;
// 命中的缓存结果
final Map hit;
// 需要重新调用源方法的keys
private Set miss;
}
然后扩展CacheManager,没什么重写,就是自定义一种manager类型 为缓存指定新的CacheManager @Primary @Bean public CacheManager getExtensionCache() { return new CacheExtensionManage("users2"); } 完整代码 https://github.com/FS1360472174/javaweb/tree/master/web/src/main/java/com/fs/web/cache
1、java.lang.IllegalStateException: Class invariant violation
at org.apache.log4j.LogManager.getLoggerRepository(LogManager.java:199)at org.apache.log4j.LogManager.getLogger(LogManager.java:228)
at o
hadoop jps出现process information unavailable提示解决办法
jps时出现如下信息:
3019 -- process information unavailable3053 -- process information unavailable2985 -- process information unavailable2917 --
<?php
class Image{
private $path;
function __construct($path='./'){
$this->path=rtrim($path,'/').'/';
}
//水印函数,参数:背景图,水印图,位置,前缀,TMD透明度
public function water($b,$l,$pos