Zebra分级缓存使用

2018-01-04  本文已影响0人  邓启翔

springboot提供cache支持,zebra扩展了此项功能,使用分级缓存:

一级缓存:

本地缓存,存放少量热点数据;

二级缓存:

远程redis缓存,可以存放大量数据,数据大小根据redis机器的内存来确定;

这样做的好处

1、少量热点数据可以缓存在本地,减少网络开销,大大提升性能;

2、应用服务器的内存是有限,在数据量很大的时候使用redis容易扩容;

zebra分级缓存使用

配置项:

zebra.cache.first=name: cacheName;option:initialCapacity=5,maximumSize=500,expireAfterWrite=100s

zebra.cache.secondary=name cacheName1;option:usedFirstCache:true,forceRefresh=false

# Redis数据库索引(默认为0)

spring.redis.database=0

# Redis服务器地址

spring.redis.host=192.168.99.100

# Redis服务器连接端口

spring.redis.port=32770

# Redis服务器连接密码(默认为空)

spring.redis.password=

spring.redis.lettuce.pool.max-active=8

spring.redis.lettuce.pool.max-wait=-1

spring.redis.lettuce.shutdown-timeout=100

spring.redis.lettuce.pool.max-idle=8

spring.redis.lettuce.pool.min-idle=0

启动添加注释

@SpringBootApplication

@ZebraConf(confName="com.guosen.examples.service.client")

@EnableAsync

@EnableCaching

public class App {

    public static void main(String[] args) throws Exception {

        ZebraRun.run(args, App.class,true);

    }

}

代码:

@Component

public class CacheService {

    @Cacheable(value = "cacheName")

    public String getName(String name) {

        System.out.println("获取值 from method");

        return name + 123;

    }

    @CachePut(value = " cacheName ", key = "#p.id")

    public String getName(Person p) {

        System.out.println("获取值 from method");

        return p.getName() + 123;

    }

    @CacheEvict(value = " cacheName ", key = "#id") // 2

     public void remove(Long id) {

            System.out.println("删除缓存");

    }

}

zebra
上一篇 下一篇

猜你喜欢

热点阅读