我正在使用spring-cache来改进数据库查询,其工作正常如下:
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager("books");
}
@Cacheable("books")
public Book getByIsbn(String isbn) {
return dao.findByIsbn(isbn);
}
Run Code Online (Sandbox Code Playgroud)
但现在我想在启动时预先填充完整的书籍缓存.这意味着我想调用dao.findAll()
并将所有值放入缓存中.此例程不应仅定期安排.
但是如何在使用时显式填充缓存@Cacheable
?
我们有休息api申请.我们使用redis进行API响应缓存和内部方法缓存.如果是redis连接,那么它会使我们的API失效.我们想绕过redis缓存,如果redis连接失败或任何异常,而不是让我们的API失效.有一个接口CacheErrorHandler,但它处理redis get set操作失败而不是redis连接问题.我们使用的是Spring 4.1.2.
计划在基于Spring网络的应用程序中实现静态数据的缓存机制,任何人都可以解释哪个是最好的以及它是如何工作的?
问题是当将 Spring 缓存与 Redis 缓存管理器一起使用时,由于没有默认构造函数,无法反序列化 Spring Pageable 响应
使用的spring boot版本是2.1.4.RELEASE
使用序列化器的 Redis 配置类
@Bean
public RedisCacheManager redisCacheManager(LettuceConnectionFactory lettuceConnectionFactory) {
RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig().disableCachingNullValues()
.serializeValuesWith(
RedisSerializationContext.SerializationPair.fromSerializer(RedisSerializer.json()));
redisCacheConfiguration.usePrefix();
return RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(lettuceConnectionFactory)
.cacheDefaults(redisCacheConfiguration).build();
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Spring 缓存和 Redis 作为缓存后端在 Redis 缓存中缓存 Spring REST API 页面结果响应
@GetMapping
@Cacheable("Article_Response_Page")
public Page<Article> findAll(Pageable pageable) {
return articleRepository.findAll(pageable);
}
Run Code Online (Sandbox Code Playgroud)
我可以Page<Article>
使用序列化程序在 Redis 缓存中看到以 JSON 形式缓存,RedisSerializer.json()
但在下一次调用期间,当从缓存中读取数据时,出现以下异常
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot
construct instance of `org.springframework.data.domain.PageImpl` (no
Creators, like default construct, exist): cannot deserialize from Object
value (no delegate- or …
Run Code Online (Sandbox Code Playgroud) fasterxml spring-boot spring-cache spring-data-commons jackson2
我在Spring应用程序中启用了缓存,并使用Redis来实现此目的.但是,每当发生连接失败时,应用程序就会停止工作,而我认为最好跳过缓存并继续执行正常的执行流程.
那么,有没有人知道如何在Spring中优雅地做到这一点?
这是我得到的例外.
Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
Run Code Online (Sandbox Code Playgroud) 我使用spring cache和this方法,它将查询的值作为JSON返回:
@RequestMapping("/getById")
@ResponseBody
@Cacheable
public HugeValue getHugeValueFromSlowFoo( @RequestParam(value = "id", defaultValue = "") String id ) {
return Foo.getById( id );
}
Run Code Online (Sandbox Code Playgroud)
这很好用,HugeValue对象存储在缓存中(在本例中为Hazelcast).我想进一步改进这个,因为从HugeValue创建JSON的时间非常长.我可以告诉spring cache缓存我对象的JSON-ified版本吗?
我使用Jackson和Spring Boot 1.2以及Spring 4.1
我正在学习Spring WebFlux,在编写示例应用程序时,我发现了与Reactive类型(Mono/Flux)结合Spring Cache的问题.
考虑以下代码片段(在Kotlin中):
@Repository
interface TaskRepository : ReactiveMongoRepository<Task, String>
@Service
class TaskService(val taskRepository: TaskRepository) {
@Cacheable("tasks")
fun get(id: String): Mono<Task> = taskRepository.findById(id)
}
Run Code Online (Sandbox Code Playgroud)
这种有效且安全的缓存方法调用是返回Mono还是Flux?也许还有其他一些原则可以做到这一点?
以下代码使用SimpleCacheResolver,但默认情况下使用Redis失败,因为Mono不是Serializable.为了使它们工作,例如需要使用Kryo序列化器.
我在tomcat 9.0.2上使用Spring Boot 1.5.9 并尝试使用spring @Cacheable调度在应用程序启动时运行的缓存刷新作业来缓存查找,并且每24小时重复一次,如下所示:
@Component
public class RefreshCacheJob {
private static final Logger logger = LoggerFactory.getLogger(RefreshCacheJob.class);
@Autowired
private CacheService cacheService;
@Scheduled(fixedRate = 3600000 * 24, initialDelay = 0)
public void refreshCache() {
try {
cacheService.refreshAllCaches();
} catch (Exception e) {
logger.error("Exception in RefreshCacheJob", e);
}
}
}
Run Code Online (Sandbox Code Playgroud)
缓存服务如下:
@Service
public class CacheService {
private static final Logger logger = LoggerFactory.getLogger(CacheService.class);
@Autowired
private CouponTypeRepository couponTypeRepository;
@CacheEvict(cacheNames = Constants.CACHE_NAME_COUPONS_TYPES, allEntries = true)
public void clearCouponsTypesCache() {} …
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring和EhCache
我有以下方法
@Override
@Cacheable(value="products", key="#root.target.PRODUCTS")
public Set<Product> findAll() {
return new LinkedHashSet<>(this.productRepository.findAll());
}
Run Code Online (Sandbox Code Playgroud)
我有其他使用@Cacheable和@CachePut以及@CacheEvict的方法.
现在,假设数据库返回100个产品并且它们被缓存key="#root.target.PRODUCTS"
,然后其他方法将插入 - 更新 - 删除项目到数据库中.因此,通过它缓存的产品key="#root.target.PRODUCTS"
不再相同,例如数据库.
我的意思是,检查以下两种方法,他们能够更新/删除一个项目,同一项目缓存在另一个key="#root.target.PRODUCTS"
@Override
@CachePut(value="products", key="#product.id")
public Product update(Product product) {
return this.productRepository.save(product);
}
@Override
@CacheEvict(value="products", key="#id")
public void delete(Integer id) {
this.productRepository.delete(id);
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以通过更新/删除位于缓存中的项目key="#root.target.PRODUCTS"
,如果产品已更新,则为100,如果删除了产品,则为499.
我的观点是,我想避免以下情况:
@Override
@CachePut(value="products", key="#product.id")
@CacheEvict(value="products", key="#root.target.PRODUCTS")
public Product update(Product product) {
return this.productRepository.save(product);
}
@Override
@Caching(evict={
@CacheEvict(value="products", key="#id"),
@CacheEvict(value="products", key="#root.target.PRODUCTS")
})
public void delete(Integer id) {
this.productRepository.delete(id); …
Run Code Online (Sandbox Code Playgroud) 我想在LoadingCache
Spring 上添加几个不同的CacheManager
,但我不知道如何使用它CaffeineCacheManager
.似乎只有一个加载器可以刷新内容,但是我需要为每个缓存单独加载.是否可以向Spring缓存管理器添加多个加载缓存?如果是这样,那怎么样?
CaffeineCacheManager cacheManage = new CaffeineCacheManager();
LoadingCache<String, Optional<Edition>> loadingCache1 =
Caffeine.newBuilder()
.maximumSize(150)
.refreshAfterWrite(5, TimeUnit.MINUTES)
.build(test -> this.testRepo.find(test));
LoadingCache<String, Optional<Edition>> loadingCache2 =
Caffeine.newBuilder()
.maximumSize(150)
.refreshAfterWrite(5, TimeUnit.MINUTES)
.build(test2 -> this.testRepo.find2(test2));
// How do I add to cache manager, and specify a name?
Run Code Online (Sandbox Code Playgroud) spring-cache ×10
spring ×6
java ×4
spring-boot ×3
caching ×2
ehcache ×2
spring-mvc ×2
caffeine ×1
fasterxml ×1
jackson2 ×1
json ×1
redis ×1
spring-3 ×1
tomcat ×1