自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Springboot 自定義注解實(shí)現(xiàn) Redis 秒級(jí)緩存

開(kāi)發(fā) 前端
cacheName屬性指定了緩存的名字,默認(rèn)為secondLevelCache;key屬性定義了緩存的鍵,默認(rèn)使用方法名作為鍵;expireInSeconds屬性定義了緩存的有效時(shí)間(秒)。

要在Spring Boot中使用自定義注解實(shí)現(xiàn)秒級(jí)緩存,可以通過(guò)以下步驟來(lái)實(shí)現(xiàn):

1. 創(chuàng)建自定義注解

首先,創(chuàng)建一個(gè)自定義注解,用于標(biāo)記需要被緩存的方法,并指定緩存的有效時(shí)間。

import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.annotation.AliasFor;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Cacheable(value = "secondLevelCache", key = "#root.methodName")
public @interface SecondLevelCacheable {
    @AliasFor(annotation = Cacheable.class, attribute = "value")
    String cacheName() default "secondLevelCache";


    @AliasFor(annotation = Cacheable.class, attribute = "key")
    String key() default "#root.methodName";


    /**
     * 緩存過(guò)期時(shí)間(秒)
     */
    int expireInSeconds() default 60;
}

這里我們定義了一個(gè)名為SecondLevelCacheable的注解,并使用@Cacheable作為元注解。cacheName屬性指定了緩存的名字,默認(rèn)為secondLevelCache;key屬性定義了緩存的鍵,默認(rèn)使用方法名作為鍵;expireInSeconds屬性定義了緩存的有效時(shí)間(秒)。

2. 配置Spring Cache

確保Spring Boot項(xiàng)目已經(jīng)配置了Spring Cache,并且啟用了Redis作為緩存存儲(chǔ)。

application.properties

spring.cache.type=redis
spring.redis.host=localhost
spring.redis.port=6379
spring:
  cache:
    type: redis
  redis:
    host: localhost
    port: 6379

3. 實(shí)現(xiàn)自定義注解處理器

接下來(lái),實(shí)現(xiàn)一個(gè)AOP切面來(lái)處理這個(gè)自定義注解。這涉及到使用Spring AOP來(lái)攔截帶有SecondLevelCacheable注解的方法調(diào)用。

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;


@Aspect
@Component
public class SecondLevelCacheAspect {


    @Autowired
    private CacheManager cacheManager;


    @Around("@annotation(secondLevelCacheable)")
    public Object handleSecondLevelCacheable(ProceedingJoinPoint joinPoint, SecondLevelCacheable secondLevelCacheable) throws Throwable {
        String cacheName = secondLevelCacheable.cacheName();
        String cacheKey = generateCacheKey(joinPoint, secondLevelCacheable.key());


        // Check if the result is in cache
        Cache cache = cacheManager.getCache(cacheName);
        ValueWrapper valueWrapper = cache.get(cacheKey);
        if (valueWrapper != null) {
            return valueWrapper.get();
        }


        // Not in cache, proceed with method execution
        Object result = joinPoint.proceed();


        // Put result into cache with expiration time
        cache.put(cacheKey, result, secondLevelCacheable.expireInSeconds(), java.util.concurrent.TimeUnit.SECONDS);
        return result;
    }


    private String generateCacheKey(ProceedingJoinPoint joinPoint, String keyExpression) {
        StringBuilder keyBuilder = new StringBuilder(keyExpression);
        for (Object arg : joinPoint.getArgs()) {
            keyBuilder.append(":").append(arg.toString());
        }
        return keyBuilder.toString();
    }
}

在這個(gè)例子中,我們定義了一個(gè)切面SecondLevelCacheAspect,它包含一個(gè)環(huán)繞通知handleSecondLevelCacheable,該通知處理所有帶有SecondLevelCacheable注解的方法調(diào)用。它首先檢查緩存中是否存在結(jié)果,如果存在則直接返回;否則執(zhí)行方法并將結(jié)果存儲(chǔ)到緩存中,并設(shè)置過(guò)期時(shí)間為expireInSeconds秒。

4. 應(yīng)用自定義注解

現(xiàn)在你可以在任何需要緩存結(jié)果的方法上應(yīng)用SecondLevelCacheable注解了。

public class SomeService {


    @SecondLevelCacheable(expireInSeconds = 30)
    public Object someMethod(Object... args) {
        // 方法邏輯
    }
}

這樣,每次調(diào)用someMethod時(shí),都會(huì)根據(jù)定義的時(shí)間檢查緩存并決定是否從緩存中獲取數(shù)據(jù)或執(zhí)行實(shí)際的方法邏輯。

以上步驟展示了如何使用自定義注解和AOP來(lái)實(shí)現(xiàn)Spring Boot中的秒級(jí)Redis緩存功能。可以根據(jù)具體需求調(diào)整注解參數(shù)和AOP邏輯。

責(zé)任編輯:武曉燕 來(lái)源: java知路
相關(guān)推薦

2024-07-02 11:42:53

SpringRedis自定義

2023-10-09 07:37:01

2023-10-11 07:57:23

springboot微服務(wù)

2023-10-24 13:48:50

自定義注解舉值驗(yàn)證

2015-07-29 10:31:16

Java緩存算法

2021-02-20 11:40:35

SpringBoot占位符開(kāi)發(fā)技術(shù)

2021-09-26 05:02:00

緩存Ehcache用法

2024-12-27 15:37:23

2021-12-30 12:30:01

Java注解編譯器

2022-02-17 07:10:39

Nest自定義注解

2017-08-03 17:00:54

Springmvc任務(wù)執(zhí)行器

2024-10-14 17:18:27

2022-12-13 09:19:06

高并發(fā)SpringBoot

2023-03-03 09:11:12

高并發(fā)SpringBoot

2023-09-04 08:12:16

分布式鎖Springboot

2025-03-05 10:49:32

2024-04-03 09:18:03

Redis數(shù)據(jù)結(jié)構(gòu)接口防刷

2009-09-07 22:00:15

LINQ自定義

2022-05-18 07:44:13

自定義菜單前端

2022-11-01 11:15:56

接口策略模式
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)