首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring 4中返回缓存中的嵌套对象

在Spring 4中返回缓存中的嵌套对象
EN

Stack Overflow用户
提问于 2017-03-09 13:43:49
回答 1查看 823关注 0票数 0

我正在按照Spring指南在这里缓存- https://spring.io/guides/gs/caching/,但我的对象有点复杂。

对于我的用例,我也有一个Author字段。这是我的整个Book课程。

代码语言:javascript
复制
public class Book {

    private String isbn;
    private String title;
    private Author author;

    public Book(String isbn, String title, Author author) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Author getAuthor() {
        return author;
    }

    public void setAuthor(Author author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return String.format("Book{isbn='%s',title='%s',author=%s}", isbn, title, author);
    }
}

现在,我添加了另一种方法来通过author id获取图书(假设只返回一本书)。这是我的SimpleBookRepository

代码语言:javascript
复制
@Component
public class SimpleBookRepository implements BookRepository {

    private List<Book> books;

    public SimpleBookRepository() {
        books = new ArrayList<>();
        books.add(new Book("1234", "Some book", new Author(1, "Author1")));
        books.add(new Book("4567", "Some another book", new Author(2, "Author2")));
    }

    @Override
    @Cacheable("books")
    public Book getByIsbn(String isbn) {
        simulateSlowService();
        return books.stream().filter(o -> o.getIsbn().equals(isbn)).findFirst().orElse(null);
    }

    @Override
    public Book getByAuthorId(int id) {
        simulateSlowService();
        return books.stream().filter(o -> o.getAuthor().getId() == id).findFirst().orElse(null);
    }

    // Don't do this at home
    private void simulateSlowService() {
        try {
            long time = 1000L;
            Thread.sleep(time);
        } catch (InterruptedException e) {
            throw new IllegalStateException(e);
        }
    }
}

现在,我想知道是否可以通过在缓存中查找Book实例从缓存中获取Author.id实例。

这是我的行刑和预期的行为。

代码语言:javascript
复制
logger.info(".... Fetching books");
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("1234"));   // Gets the original object
logger.info("isbn-4567 -->" + bookRepository.getByIsbn("4567"));   // Gets the original object
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("1234"));   // Should fetch from cache - WORKS FINE
logger.info("isbn-4567 -->" + bookRepository.getByIsbn("4567"));   // Should fetch from cache - WORKS FINE
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("1234"));   // Should fetch from cache - WORKS FINE
logger.info("isbn-1234 -->" + bookRepository.getByIsbn("1234"));   // Should fetch from cache - WORKS FINE

logger.info(".... Fetching books by author");
logger.info("author-1 -->" + bookRepository.getByAuthorId(1));   // Should fetch from cache - NOT WORKING
logger.info("author-2 -->" + bookRepository.getByAuthorId(2));   // Should fetch from cache - NOT WORKING
logger.info("author-1 -->" + bookRepository.getByAuthorId(1));   // Should fetch from cache - NOT WORKING
logger.info("author-2 -->" + bookRepository.getByAuthorId(2));   // Should fetch from cache - NOT WORKING
logger.info("author-1 -->" + bookRepository.getByAuthorId(1));   // Should fetch from cache - NOT WORKING
logger.info("author-1 -->" + bookRepository.getByAuthorId(1));   // Should fetch from cache - NOT WORKING

有没有办法做到这一点,或者我需要另一个缓存用于这个目的?

EN

回答 1

Stack Overflow用户

发布于 2017-03-09 14:18:47

你不需要另一个缓存。只需在getByAuthorId方法中指定要缓存该方法。Spring每次调用该方法时,都会在缓存中创建一个带有键author、id和value Book的条目,如果该方法还不存在的话。只需将@Cacheable("books")添加到getByAuthorId方法即可。

因此,现在在您的books缓存中,将有包含isbn键和映射到Books的author.id键的条目。

的响应是,如果在调用getByIsbn()时将实例添加到缓存中,我希望能够在调用getBookByAuthorId()时使用相同的实例--我能想到的唯一解决方案是手动将条目添加到缓存中,例如:

代码语言:javascript
复制
@Component
public class SimpleBookRepository implements BookRepository {

    // inject this bean
    private CacheManager cacheManager;

    @Override
    @Cacheable("books")
    public Book getByIsbn(String isbn) {
        simulateSlowService();
        Book b = books.stream().filter(o -> o.getIsbn().equals(isbn)).findFirst().orElse(null);

        if (b != null && b.getAuthor() != null && b.getAuthor().getId() != null) {
            Cache cache = cacheManager.getCache("books");
            cache.put(b.getAuthor().getId(), b);
        }

        return b;
    }

    // ......
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42696995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档