最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot配置Redis自定義過期時(shí)間操作

 更新時(shí)間:2021年07月28日 11:19:15   作者:悅瀾群書  
這篇文章主要介紹了SpringBoot配置Redis自定義過期時(shí)間操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot配置Redis自定義過期時(shí)間

Redis配置依賴

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-redis</artifactId>
        <version>1.4.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.8.1.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.9.0</version>
</dependency>

SpringBoot-Reids配置文件

package com.regs.tms.common.redis;
@Configuration
@EnableCaching// 啟用緩存,這個(gè)注解很重要
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedisCacheConfig extends CachingConfigurerSupport {
	private String host;
	private Integer port;
	private Integer database;
	private String password;

	@Bean("redisTemplate")
	public RedisTemplate redisTemplate(RedisConnectionFactory factory) {
	    StringRedisTemplate template = new StringRedisTemplate();
	    template.setConnectionFactory(factory);
	    //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值
	    Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
	    ObjectMapper mapper = new ObjectMapper();
	    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
	    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
	    serializer.setObjectMapper(mapper);
	    template.setValueSerializer(serializer);
	    template.setHashValueSerializer(serializer);
	    // 設(shè)置鍵(key)的序列化采用StringRedisSerializer。
	    template.setKeySerializer(new StringRedisSerializer());
	    template.setHashKeySerializer(new StringRedisSerializer());
	    //打開事務(wù)支持
	    template.setEnableTransactionSupport(true);
	    template.afterPropertiesSet();
	    return template;
	}

	@Bean
	public PlatformTransactionManager transactionManager(DataSource dataSource) throws SQLException {
	    //配置事務(wù)管理器
	    return new DataSourceTransactionManager(dataSource);
	}

	@Bean("stringRedisTemplate")
	public StringRedisTemplate stringRedisTemplate() {
	    Integer port = this.port == null ? 6379 : this.port;
	    JedisConnectionFactory jedis = new JedisConnectionFactory();
	    jedis.setHostName(host);
	    jedis.setPort(port);
	    if (StringUtils.isNotEmpty(password)) {
	        jedis.setPassword(password);
	    }
	    if (database != null) {
	        jedis.setDatabase(database);
	    } else {
	        jedis.setDatabase(0);
	    }
	    // 初始化連接pool
	    jedis.afterPropertiesSet();
	    // 獲取連接template
	    StringRedisTemplate temple = new StringRedisTemplate();
	    temple.setConnectionFactory(jedis);
	    return temple;
	}
}

自定義失效注解

package com.regs.tms.common.redis.annotation;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface CacheDuration {
    //Sets the expire time (in seconds).
    public long duration() default 60;
}

自定義失效配置

package com.regs.tms.common.redis.annotation;
 /**
 * ExpireCacheManager,繼承自RedisCacheManager,
 * 用于對(duì)@CacheExpire解析及有效期的設(shè)置
 */
public class RedisExpireCacheManager extends RedisCacheManager implements ApplicationContextAware, InitializingBean {
    private ApplicationContext applicationContext;

    public RedisExpireCacheManager(RedisTemplate redisTemplate) {
        super(redisTemplate);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() {
        parseCacheExpire(applicationContext);
    }

    private void parseCacheExpire(ApplicationContext applicationContext) {
        final Map<String, Long> cacheExpires = new HashMap<>(16);
        //掃描有注解
        String[] beanNames = applicationContext.getBeanNamesForAnnotation(Cacheable.class);
        for (String beanName : beanNames) {
            final Class clazz = applicationContext.getType(beanName);
            addCacheExpires(clazz, cacheExpires);
        }
        //設(shè)置有效期
        super.setExpires(cacheExpires);
    }

    private void addCacheExpires(final Class clazz, final Map<String, Long> cacheExpires) {
        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(method);
                //根據(jù)CacheExpire注解獲取時(shí)間
                CacheExpire cacheExpire = findCacheExpire(clazz, method);
                if (cacheExpire != null) {
                    Cacheable cacheable = findAnnotation(method, Cacheable.class);
                    String[] cacheNames = isEmpty(cacheable.value()) ? new String[]{} : cacheable.value();
                    for (String cacheName : cacheNames) {
                        cacheExpires.put(cacheName, cacheExpire.expire());
                    }
                }
            }
        }, new ReflectionUtils.MethodFilter() {
            @Override
            public boolean matches(Method method) {
                return null != findAnnotation(method, Cacheable.class);
            }
        });
    }

    /**
     * CacheExpire標(biāo)注的有效期,優(yōu)先使用方法上標(biāo)注的有效期
     *
     * @param clazz
     * @param method
     * @return
     */
    private CacheExpire findCacheExpire(Class clazz, Method method) {
        CacheExpire methodCache = findAnnotation(method, CacheExpire.class);
        if (null != methodCache) {
            return methodCache;
        }
        CacheExpire classCache = findAnnotation(clazz, CacheExpire.class);
        if (null != classCache) {
            return classCache;
        }
        return null;
    }
}

spring boot 使用redis 超時(shí)時(shí)間重新設(shè)置

如果要計(jì)算每24小時(shí)的下單量,

通常的做法是,取出舊值,進(jìn)行加一在設(shè)置回去,

但是這樣就出現(xiàn)了一個(gè)問題

第二次設(shè)置值的時(shí)候,把超時(shí)時(shí)間重新設(shè)置成個(gè)24小時(shí)

這樣無疑的記錄24小時(shí)的數(shù)量是不準(zhǔn)確的

并且spring boot 中,默認(rèn)使用了spring 來操作redis ,使存在每個(gè)redis中的值,都會(huì)加前面加入一些東西

1) "\xac\xed\x00\x05t\x00\x0bREDISUALIST"

我們?cè)诓檎颐總€(gè)值的時(shí)候,并不知道在key前面需要加點(diǎn)什么.

所以我們必須要用keys 這個(gè)命令 ,來匹配 我們需要查找的key,來取第一個(gè)

然后我們用 ttl 命令 返回指定key的剩余時(shí)間 ,重新設(shè)置回去,而不是設(shè)置24小時(shí),這樣就實(shí)現(xiàn)了24小時(shí)累加一次

在redisService 中,增加一個(gè)方法

/**
     * 獲取指定key的剩余超時(shí)時(shí)間,key最好是唯一的,有特點(diǎn)的,最好不要匹配出多個(gè) 例子 *111 取出 "\xac\xed\x00\x05t\x00\x0b111"
     * 返回剩余秒數(shù)
     * @param key
     * @return
     * create by jcd
     */
    public Long ttlByKey(@NotNull String key){
        Set<byte[]> keys = redisTemplate.getConnectionFactory().getConnection().keys(key.getBytes());
        byte[] bytes = keys.stream().findFirst().get();
        Long ttl = redisTemplate.getConnectionFactory().getConnection().ttl(bytes);
        return ttl;
    }

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot沒有主清單屬性的解決方法

    SpringBoot沒有主清單屬性的解決方法

    在本篇文章里小編給大家整理的是關(guān)于解決SpringBoot沒有主清單屬性知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-11-11
  • 關(guān)于Idea使用git時(shí)commit特別慢的問題及解決方法

    關(guān)于Idea使用git時(shí)commit特別慢的問題及解決方法

    這篇文章主要介紹了關(guān)于Idea使用git時(shí)commit特別慢的問題及解決方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密相關(guān)代碼

    SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密相關(guān)代碼

    這篇文章主要介紹了SpringBoot項(xiàng)目配置數(shù)據(jù)庫(kù)密碼加密的相關(guān)資料,本文介紹了在Springboot項(xiàng)目中配置數(shù)據(jù)庫(kù)連接時(shí)存在的安全問題,即用戶名和密碼以明文形式存儲(chǔ),容易泄露,提出了一種簡(jiǎn)單的加密方案,需要的朋友可以參考下
    2024-11-11
  • java如何解析/讀取xml文件

    java如何解析/讀取xml文件

    這篇文章主要為大家詳細(xì)介紹了java如何解析/讀取xml文件的方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Java?ObjectMapper的使用和使用過程中遇到的問題

    Java?ObjectMapper的使用和使用過程中遇到的問題

    在Java開發(fā)中,ObjectMapper是Jackson庫(kù)的核心類,用于將Java對(duì)象序列化為JSON字符串,或者將JSON字符串反序列化為Java對(duì)象,這篇文章主要介紹了Java?ObjectMapper的使用和使用過程中遇到的問題,需要的朋友可以參考下
    2024-07-07
  • idea導(dǎo)入springboot項(xiàng)目沒有maven的解決

    idea導(dǎo)入springboot項(xiàng)目沒有maven的解決

    這篇文章主要介紹了idea導(dǎo)入springboot項(xiàng)目沒有maven的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • 談?wù)凧ava中整數(shù)類型(short int long)的存儲(chǔ)方式

    談?wù)凧ava中整數(shù)類型(short int long)的存儲(chǔ)方式

    在java中的整數(shù)類型有四種,分別是byte short in long,本文重點(diǎn)給大家介紹java中的整數(shù)類型(short int long),由于byte只是一個(gè)字節(jié)0或1,在此就不多說了,對(duì)java中的整數(shù)類型感興趣的朋友一起學(xué)習(xí)吧
    2015-11-11
  • JavaBean四個(gè)作用域范圍的詳解

    JavaBean四個(gè)作用域范圍的詳解

    這篇文章主要介紹了JavaBean四個(gè)作用域范圍的詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • java多線程的同步方法實(shí)例代碼

    java多線程的同步方法實(shí)例代碼

    這篇文章主要介紹了 java多線程的同步方法實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Mybatis圖文并茂講解分頁(yè)插件

    Mybatis圖文并茂講解分頁(yè)插件

    使用過mybatis的人都知道,mybatis本身就很小且簡(jiǎn)單,sql寫在xml里,統(tǒng)一管理和優(yōu)化。缺點(diǎn)當(dāng)然也有,比如我們使用過程中,要使用到分頁(yè),如果用最原始的方式的話,1.查詢分頁(yè)數(shù)據(jù),2.獲取分頁(yè)長(zhǎng)度,也就是說要使用到兩個(gè)方法才能完成分頁(yè)
    2022-07-07

最新評(píng)論

津南区| 海门市| 达州市| 塔河县| 博野县| 喀喇| 九龙县| 永善县| 双牌县| 惠水县| 秦皇岛市| 吉木萨尔县| 岑巩县| 贵溪市| 永川市| 延吉市| 兰溪市| 余干县| 石棉县| 和田县| 麻栗坡县| 纳雍县| 五河县| 义乌市| 曲阜市| 陆河县| 宁陕县| 泗洪县| 梁河县| 荣成市| 中西区| 宁海县| 萝北县| 乐山市| 富蕴县| 青河县| 化德县| 云阳县| 丹东市| 博湖县| 新密市|