Search in sources :

Example 1 with Book

use of com.hry.spring.redis.cache.support.Book in project spring_boot by hryou0922.

the class BookService method queryBookCacheableByBookQry.

/**
 * 缓存的key也可以指定对象的成员变量
 * @param qry
 * @return
 */
@Cacheable(cacheNames = "book1", key = "#qry.id")
public Book queryBookCacheableByBookQry(BookQry qry) {
    logger.info("queryBookCacheableByBookQry,qry={}", qry);
    String id = qry.getId();
    Assert.notNull(id, "id can't be null!");
    String name = qry.getName();
    Book book = null;
    if (id != null) {
        book = repositoryBook.get(id);
        if (book != null && !(name != null && book.getName().equals(name))) {
            book = null;
        }
    }
    return book;
}
Also used : Book(com.hry.spring.redis.cache.support.Book) Cacheable(org.springframework.cache.annotation.Cacheable)

Example 2 with Book

use of com.hry.spring.redis.cache.support.Book in project spring_boot by hryou0922.

the class AbstractService method init.

@PostConstruct
public void init() {
    // 1
    Book book1 = new Book("1", "name_1", 11, new Date());
    repositoryBook.put(book1.getId(), book1);
    // 2
    Book book2 = new Book("2", "name_2", 11, new Date());
    repositoryBook.put(book2.getId(), book2);
    // 3
    Book book3 = new Book("3", "name_3", 11, new Date());
    repositoryBook.put(book3.getId(), book3);
    // 4
    Book book4 = new Book("4", "name_4", 11, new Date());
    repositoryBook.put(book4.getId(), book4);
}
Also used : Book(com.hry.spring.redis.cache.support.Book) Date(java.util.Date) PostConstruct(javax.annotation.PostConstruct)

Example 3 with Book

use of com.hry.spring.redis.cache.support.Book in project spring_boot by hryou0922.

the class BookService method updateBook.

/**
 * 对符合key条件的记录从缓存中book1移除
 */
@CacheEvict(cacheNames = "book1", key = "#id")
public void updateBook(String id, String name) {
    logger.info("updateBook");
    Book book = repositoryBook.get(id);
    if (book != null) {
        book.setName(name);
        book.setUpdate(new Date());
    }
}
Also used : Book(com.hry.spring.redis.cache.support.Book) Date(java.util.Date) CacheEvict(org.springframework.cache.annotation.CacheEvict)

Aggregations

Book (com.hry.spring.redis.cache.support.Book)3 Date (java.util.Date)2 PostConstruct (javax.annotation.PostConstruct)1 CacheEvict (org.springframework.cache.annotation.CacheEvict)1 Cacheable (org.springframework.cache.annotation.Cacheable)1