spring-cache(1)

spring cache学习从demo开始,本章提供一个简易版的spring cache使用。demo演示为maven依赖。

1、引入jar包

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.3.RELEASE</version>
        </dependency>

2、编码

package pers.growing.cache;

/**
 * 常量定义
 */
public class CacheConstant {

    public final static String CACHEMANAGER="cacheManagerName"; //cacheManager的名称

    public final static String KEYGENERATOR="keyGeneratorName"; //cache中对应的key生成器的名称

    public final static String CACHENAME="cacheName"; //cache名称
}

package pers.growing.cache;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * spring 注解形式的配置
 */
@Configuration
@ComponentScan("pers.growing.cache")
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {

    @Bean(CacheConstant.CACHEMANAGER)
    @Override
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        List<ConcurrentMapCache> caches = new ArrayList<ConcurrentMapCache>();
        ConcurrentMapCache cache = new ConcurrentMapCache(CacheConstant.CACHENAME);
        caches.add(cache);
        cacheManager.setCaches(caches);
        return cacheManager;
    }

    @Bean(CacheConstant.KEYGENERATOR)
    @Override
    public KeyGenerator keyGenerator() {
        return new SimpleKeyGenerator();
    }
   
}
package pers.growing.cache;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * 具体的cache操作
 */
@Service
public class CacheOperate {

    @Cacheable(cacheManager = CacheConstant.CACHEMANAGER, keyGenerator = CacheConstant.KEYGENERATOR, value = CacheConstant.CACHENAME)
    public String query(String name, int age) {
        System.out.println("---query---");
        //TODO 从某一个数据源获取数据
        return name + age;
    }

    @CachePut(cacheManager = CacheConstant.CACHEMANAGER, keyGenerator = CacheConstant.KEYGENERATOR, value = CacheConstant.CACHENAME)
    public String put(String name, int age) {
        System.out.println("---put---");
        //TODO 从某一个数据源获取数据
        return name + age;
    }

    @CacheEvict(cacheManager = CacheConstant.CACHEMANAGER, keyGenerator = CacheConstant.KEYGENERATOR, value = CacheConstant.CACHENAME)
    public void evict(String name, int age) {
        System.out.println("---evict---");
        //TODO 清除满足key的数据
    }
}

3、测试代码(常见的cache使用方式)

3.1通过spring代理方式调用cache

package pers.growing.cache;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 通过spring 代理方式调用
 */
public class TestOne {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(CacheConfig.class);
        CacheOperate cs = context.getBean("cacheOperate", CacheOperate.class);

        String name = "cname";
        int age = 20;

        System.out.println("验证@Cacheable");
        //@Cacheable 第一次访问方法,会进入方法内部访问,然后将访问结果缓存起来,之后就从缓存中获取数据
        for (int i = 0; i < 10; i++) {
            System.out.println("call" + i + "----" + cs.query(name, age));
        }

        System.out.println("验证@CacheEvict");
        //@CacheEvict 清除cache
        cs.evict(name, age);

        for (int i = 0; i < 10; i++) {
            System.out.println("call" + i + "----" + cs.query(name, age));
        }

        System.out.println("验证@CachePut");
        //@CacheEvict 清除cache
        cs.evict(name, age);
        //@CachePut 将查出来的数据放到cache
        cs.put(name, age);
        for (int i = 0; i < 10; i++) { //不会再从@Cacheable对应方法内部获取数据
            System.out.println("call" + i + "----" + cs.query(name, age));
        }

    }
}

 测试结果

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
验证@Cacheable
---query---
call0----cname20
call1----cname20
call2----cname20
call3----cname20
call4----cname20
call5----cname20
call6----cname20
call7----cname20
call8----cname20
call9----cname20
验证@CacheEvict
---evict---
---query---   //由于上一行为 ---evict---,cache中对应的key已经被清除了
call0----cname20
call1----cname20
call2----cname20
call3----cname20
call4----cname20
call5----cname20
call6----cname20
call7----cname20
call8----cname20
call9----cname20
验证@CachePut
---evict---
---put---
call0----cname20 //因为已经有 ---put---,所以就不会出现 ---query---
call1----cname20
call2----cname20
call3----cname20
call4----cname20
call5----cname20
call6----cname20
call7----cname20
call8----cname20
call9----cname20

Process finished with exit code 0

3.2 通过cacheManager使用cache

package pers.growing.cache;

import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.util.Assert;

/**
 * 通过获取到cacheManager直接操作cache
 */
public class TestTwo {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(CacheConfig.class);
        SimpleCacheManager manager = context.getBean(CacheConstant.CACHEMANAGER, SimpleCacheManager.class);

        String key = "cname";
        int value = 20;

        //放入到cache
        manager.getCache(CacheConstant.CACHENAME).put(key, value);

        //从cache中获取数据
        for (int i = 0; i < 10; i++) {
            Object store = manager.getCache(CacheConstant.CACHENAME).get(key).get();

            Assert.isTrue(value == (Integer) store, "cache验证失败");
        }

    }
}

测试结果

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Process finished with exit code 0

4、总结

本章介绍了简单的cache配置,以及常见的cache使用方式。之后章节会逐步讲解spring cache的实现。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值