ehcache 使用示例


ehcache 使用示例

 

 

*************************

示例

 

*****************

config 层

 

EhCacheConfig

@EnableCaching
@Configuration
public class EhCacheConfig {

    @Bean
    public CacheManager initCacheManager(){
        EhcacheCachingProvider cachingProvider=(EhcacheCachingProvider) Caching.getCachingProvider(EhcacheCachingProvider.class.getName());

        DefaultConfiguration configuration=new DefaultConfiguration(cachingProvider.getDefaultClassLoader(),
                new DefaultPersistenceConfiguration(new File("d:"+File.separator+"cache")));
        configuration.addCacheConfiguration("custom",CacheConfigurationBuilder
                .newCacheConfigurationBuilder(Integer.class,String.class,
                        ResourcePoolsBuilder.newResourcePoolsBuilder()
                        .heap(10, MemoryUnit.MB)
                        .disk(100,MemoryUnit.MB)
                        .build()).build());

        return cachingProvider.getCacheManager(cachingProvider.getDefaultURI(),configuration);
    }
}

说明:使用@EnableCaching开启缓存功能

 

*****************

service 层

 

CacheService

public interface CacheService {

    String put(Integer id,String value);
    String get(Integer id);
    String update(Integer id,String value);
    void delete(Integer id);
}

 

*****************

serviceImpl 层

 

CacheServiceImpl

@Service
public class CacheServiceImpl implements CacheService {

    private final Map<Integer,String> map=new HashMap<>();

    @Override
    @Cacheable(value = "custom",key = "#id")
    public String get(Integer id) {
        System.out.println("查询数据:"+id);

        String result=map.get(id);
        if (result==null){
            return "查询的数据不存在";
        }

        return result;
    }

    @Override
    @CachePut(value = "custom",key = "#id")
    public String put(Integer id, String value) {
        System.out.println("插入数据:"+id+" ==> "+value);
        map.put(id,value);

        return value;
    }

    @Override
    @CachePut(value = "custom",key = "#id")
    public String update(Integer id, String value) {
        System.out.println("更新数据:"+id+" ==> "+value);
        map.put(id,value);

        return value;
    }

    @CacheEvict(value = "custom",key = "#id")
    public void delete(Integer id){
        System.out.println("删除数据:"+id);
        map.remove(id);
    }
}

 

*****************

controller 层

 

HelloController

@RestController
public class HelloController {

    @Resource
    private CacheService cacheService;

    @RequestMapping("/get")
    public String get(Integer id){
        return cacheService.get(id);
    }

    @RequestMapping("/put")
    public String put(Integer id,String name){
        cacheService.put(id, name);

        return "success";
    }

    @RequestMapping("/update")
    public String update(Integer id,String name){
        cacheService.update(id,name);

        return name;
    }

    @RequestMapping("/delete")
    public String delete(Integer id){
        cacheService.delete(id);

        return "success";
    }
}

 

 

*************************

控制台输出

 

localhost:8080/get?id=1                                      ==> 输出:查询的数据不存在

localhost:8080/put?id=1&name=瓜田李下         ==> 输出:success

localhost:8080/get?id=1                                      ==> 输出:瓜田李下

查询数据:1
插入数据:1 ==> 瓜田李下

第一次查询:缓存内没有数据,执行方法查询数据

第二次查询:执行put操作后,缓存内添加数据,直接返回缓存数据,不执行方法

 

localhost:8080/get?id=1                                      ==> 输出:瓜田李下

localhost:8080/update?id=1&name=海贼王       ==> 输出:海贼王

localhost:8080/get?id=1                                       ==> 输出:海贼王

更新数据:1 ==> 海贼王

第一次查询:使用缓存,没有执行方法

第二次查询:更新数据时,同时更新缓存数据,从缓存内查找数据,没有执行方法

 

localhost:8080/get?id=1                                      ==> 输出:海贼王

localhost:8080/delete?id=1                                 ==> 输出:success

localhost:8080/get?id=1                                       ==> 输出:查询的数据不存在

删除数据:1

第一次查询:使用缓存,没有执行方法

第二次查询:缓存没数据删除,执行方法,map内数据也删除了,数据不存在

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值