Memcached + SpringBoot 从一个常量文件中读取缓存键值 [英] Memcached + SpringBoot read cache key value from a constant file

查看:64
本文介绍了Memcached + SpringBoot 从一个常量文件中读取缓存键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在缓存和驱逐数据时从 java 常量文件中读取 memcached 密钥.我不想从属性文件中读取,只是从常量文件中读取.

例如在缓存数据时:

<块引用>

BookConstant.java

公共类 BookConstant{public static final String CACHE_BY_BOOK_ID = "book_id";}

<块引用>

BookServiceImpl.java

@Override@Cacheable(cacheNames = BookConstant.CACHE_NAME, key = "#BookConstant.CACHE_BY_BOOK_ID + '_' + #id")公共图书 getById(Long id){可选的<书>book = bookRepository.findById(id);返回 book.orElse(null);}

我的目的是生成密钥:

<块引用>

book_id_1234

在驱逐数据时:

@Overridepublic Book updateBook(Book book){book.setModifiedDate(new Date());Book newBook = bookRepository.save(book);bookSearchService.insertBookIndex(newBook);memcachedClient.evict(BookConstant.CACHE_BY_BOOK_ID + "_" + book.getId());返回新书;}

所以它在缓存数据时抛出错误:

<块引用>

SpelEvaluationException: EL1007E: 在 null 上找不到属性或字段CACHE_BY_BOOK_ID"

因为它尝试从属性文件中读取密钥.

我不确定是否可以动态地从常量文件中读取密钥,但这就是问题所在.如果可能的话,你能帮我吗?谢谢.

解决方案

SpEL 用于访问 @Cacheable 注释中使用的常量值是不正确的.您需要使用包指定完整的类名,例如如果你的包是 com.example 然后像这样指定它:

@Cacheable(cacheNames = BookConstant.CACHE_NAME, key = "T(com.example.BookConstant).CACHE_BY_BOOK_ID + '_' + #id")公共书 getById(Long id) {//...}

对于更新,您不需要直接使用 memcachedClient 驱逐,而是使用 @CacheEvict 注释,例如

@CacheEvict(cacheNames = BookConstant.CACHE_NAME, key = "T(com.example.BookConstant).CACHE_BY_BOOK_ID + '_' + #book.id")public Book updateBook(Book book) {//...}

I wanna read memcached key from a java constant file while caching and evicting data. I don't wanna read from a property file, just read from a constant file.

For example while caching data:

BookConstant.java

public class BookConstant
{
    public static final String CACHE_BY_BOOK_ID = "book_id";
}

BookServiceImpl.java

@Override
@Cacheable(cacheNames = BookConstant.CACHE_NAME, key = "#BookConstant.CACHE_BY_BOOK_ID + '_' + #id")
public Book getById(Long id)
{
    Optional<Book> book = bookRepository.findById(id);
    return book.orElse(null);
}

My purpose is generate key :

book_id_1234

And while evicting data:

@Override
public Book updateBook(Book book)
{
    book.setModifiedDate(new Date());
    Book newBook = bookRepository.save(book);

    bookSearchService.insertBookIndex(newBook);

    memcachedClient.evict(BookConstant.CACHE_BY_BOOK_ID + "_" + book.getId());

    return newBook;
}

So it's throwing error while caching data:

SpelEvaluationException: EL1007E: Property or field 'CACHE_BY_BOOK_ID' cannot be found on null

Because it tries read key from a property file.

I'm not sure is possible or not read key from a constant file as dynamically, but that's the problem. If it's possible, can you help me please? Thanks.

解决方案

The SpEL for accessing the constant value used in @Cacheable annotation is incorrect. You need to specify the full class name, with the package e.g. if your package is com.example then specify it like this:

@Cacheable(cacheNames = BookConstant.CACHE_NAME, key = "T(com.example.BookConstant).CACHE_BY_BOOK_ID + '_' + #id")
public Book getById(Long id) {
    //...
}

For the update, you don't need to evict using the memcachedClient directly, but instead use the @CacheEvict annotation e.g.

@CacheEvict(cacheNames = BookConstant.CACHE_NAME, key = "T(com.example.BookConstant).CACHE_BY_BOOK_ID + '_' + #book.id")
public Book updateBook(Book book) {
    // ...
}

这篇关于Memcached + SpringBoot 从一个常量文件中读取缓存键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆