springboot提供cache支持搬卒,zebra擴(kuò)展了此項(xiàng)功能瑟俭,使用分級(jí)緩存:
一級(jí)緩存:
本地緩存,存放少量熱點(diǎn)數(shù)據(jù)契邀;
二級(jí)緩存:
遠(yuǎn)程redis緩存摆寄,可以存放大量數(shù)據(jù),數(shù)據(jù)大小根據(jù)redis機(jī)器的內(nèi)存來確定坯门;
這樣做的好處
1微饥、少量熱點(diǎn)數(shù)據(jù)可以緩存在本地,減少網(wǎng)絡(luò)開銷古戴,大大提升性能欠橘;
2、應(yīng)用服務(wù)器的內(nèi)存是有限现恼,在數(shù)據(jù)量很大的時(shí)候使用redis容易擴(kuò)容肃续;
zebra分級(jí)緩存使用
配置項(xiàng):
zebra.cache.first=name: cacheName;option:initialCapacity=5,maximumSize=500,expireAfterWrite=100s
zebra.cache.secondary=name cacheName1;option:usedFirstCache:true,forceRefresh=false
# Redis數(shù)據(jù)庫(kù)索引(默認(rèn)為0)
spring.redis.database=0
# Redis服務(wù)器地址
spring.redis.host=192.168.99.100
# Redis服務(wù)器連接端口
spring.redis.port=32770
# Redis服務(wù)器連接密碼(默認(rèn)為空)
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
啟動(dòng)添加注釋
@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("刪除緩存");
????}
}