亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot redis分布式緩存實(shí)現(xiàn)過程解析

 更新時(shí)間:2019年10月31日 10:40:16   作者:我想和這個(gè)世界談?wù)?  
這篇文章主要介紹了SpringBoot redis分布式緩存實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

前言

應(yīng)用系統(tǒng)需要通過Cache來緩存不經(jīng)常改變得數(shù)據(jù)來提高系統(tǒng)性能和增加系統(tǒng)吞吐量,避免直接訪問數(shù)據(jù)庫等低速存儲(chǔ)系統(tǒng)。緩存的數(shù)據(jù)通常存放在訪問速度更快的內(nèi)存里或者是低延遲存取的存儲(chǔ)器,服務(wù)器上。應(yīng)用系統(tǒng)緩存,通常有如下作用:緩存web系統(tǒng)的輸出,如偽靜態(tài)頁面。緩存系統(tǒng)的不經(jīng)常改變的業(yè)務(wù)數(shù)據(jù),如用戶權(quán)限,字典數(shù)據(jù).配置信息等

大家都知道springBoot項(xiàng)目都是微服務(wù)部署,A服務(wù)和B服務(wù)分開部署,那么它們?nèi)绾胃禄蛘攉@取共有模塊的緩存數(shù)據(jù),或者給A服務(wù)做分布式集群負(fù)載,如何確保A服務(wù)的所有集群都能同步公共模塊的緩存數(shù)據(jù),這些都涉及到分布式系統(tǒng)緩存的實(shí)現(xiàn)。(ehcache可以通過Terracotta組件一個(gè)緩存集群,這個(gè)暫時(shí)不講)

但是ehcache的設(shè)計(jì)并不適合做分布式緩存,所以今天用redis來實(shí)現(xiàn)分布式緩存。

架構(gòu)圖:

一二級(jí)緩存服務(wù)器

使用Redis緩存,通過網(wǎng)絡(luò)訪問還是不如從內(nèi)存中獲取性能好,所以通常稱之為二級(jí)緩存,從內(nèi)存中取得的緩存數(shù)據(jù)稱之為一級(jí)緩存。當(dāng)應(yīng)用系統(tǒng)需要查詢緩存的時(shí)候,先從一級(jí)緩存里查找,如果有,則返回,如果沒有查找到,則再查詢二級(jí)緩存,架構(gòu)圖如下

Spring Boot 2 自帶了前面?zhèn)z種緩存的實(shí)現(xiàn)方式,本文將簡單實(shí)現(xiàn)第三種,高速一二級(jí)緩存實(shí)現(xiàn)

Redis分布式緩存

引入redis的starter

配置Redis

在application.yml中配置redis信息

spring:
 redis:
  database: 0
  host: 192.168.0.146
  port: 6379
  timeout: 5000

其他相關(guān)配置

# Redis數(shù)據(jù)庫索引(默認(rèn)為0)
spring.redis.database=0 
# Redis服務(wù)器地址
spring.redis.host=127.0.0.1
# Redis服務(wù)器連接端口
spring.redis.port=6379 
# Redis服務(wù)器連接密碼(默認(rèn)為空)
spring.redis.password=
# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制)
spring.redis.pool.max-active=8 
# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制)
spring.redis.pool.max-wait=-1 
# 連接池中的最大空閑連接
spring.redis.pool.max-idle=8 
# 連接池中的最小空閑連接
spring.redis.pool.min-idle=0 
# 連接超時(shí)時(shí)間(毫秒)
spring.redis.timeout=0 

配置Redis緩存序列化機(jī)制

有的時(shí)候需要將對(duì)象存進(jìn)redis(例如一個(gè)JavaBean對(duì)象),但是如果對(duì)象不是可Serializable的,因此需要讓JavaBean對(duì)象實(shí)現(xiàn)Serializable接口

public class UserPO implements Serializable {

如果只是讓JavaBean實(shí)現(xiàn)Serializable接口也是可以存儲(chǔ)的,但是并不好看,那么能不能將JavaBean弄成Json的樣式放進(jìn)redis呢。直接的方式就是自己轉(zhuǎn)換,但是未免有點(diǎn)麻煩,那就只能修改RedisTemplate的序列化機(jī)制了,在配置類中配置上序列化的方法即可

@Bean
 public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
 
   RedisTemplate<String, Object> template = new RedisTemplate();
   template.setConnectionFactory(factory);
 
   Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
   ObjectMapper om = new ObjectMapper();
   om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
   om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
   jacksonSeial.setObjectMapper(om);
 
   // 值采用json序列化
   template.setValueSerializer(jacksonSeial);
   //使用StringRedisSerializer來序列化和反序列化redis的key值
   template.setKeySerializer(new StringRedisSerializer());
   template.setHashKeySerializer(new StringRedisSerializer());
   template.setHashValueSerializer(jacksonSeial);
   template.afterPropertiesSet();
 
   return template;
 }

自定義CacheManager

/**
  * 選擇Redis作為默認(rèn)緩存工具
  * @param redisTemplate
  * @return
  */
 @Bean
 public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
 
   RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
   return RedisCacheManager
       .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
       .cacheDefaults(redisCacheConfiguration).build();
 
 }

RedisConfig完整代碼

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
 
import java.lang.reflect.Method;
import java.net.UnknownHostException;
 
 
 
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
 
  /**
   * 選擇Redis作為默認(rèn)緩存工具
   * @param redisTemplate
   * @return
   */
  @Bean
  public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
 
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
    return RedisCacheManager
        .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
        .cacheDefaults(redisCacheConfiguration).build();
 
  }
 
 
  /**
   * retemplate相關(guān)配置(序列化機(jī)制對(duì)象)
   * @param factory
   * @return
   */
  @Bean
  public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
 
    RedisTemplate<String, Object> template = new RedisTemplate();
    // 配置連接工廠
    template.setConnectionFactory(factory);
 
    //使用Jackson2JsonRedisSerializer來序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
    Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
 
    ObjectMapper om = new ObjectMapper();
    // 指定要序列化的域,field,get和set,以及修飾符范圍,ANY是都有包括private和public
    om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
    // 指定序列化輸入的類型,類必須是非final修飾的,final修飾的類,比如String,Integer等會(huì)跑出異常
    om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jacksonSeial.setObjectMapper(om);
 
    // 值采用json序列化
    template.setValueSerializer(jacksonSeial);
    //使用StringRedisSerializer來序列化和反序列化redis的key值
    template.setKeySerializer(new StringRedisSerializer());
 
    // 設(shè)置hash key 和value序列化模式
    template.setHashKeySerializer(new StringRedisSerializer());
    template.setHashValueSerializer(jacksonSeial);
    template.afterPropertiesSet();
 
    return template;
  }
 
 
  /**
   * 自定義key的生成策略
   * @return
   */
  @Bean
  public KeyGenerator myKeyGenerator(){
    return new KeyGenerator() {
      @Override
      public Object generate(Object target, Method method, Object... params) {
        StringBuilder sb = new StringBuilder();
        sb.append(target.getClass().getName());
        sb.append(method.getName());
        for (Object obj : params) {
          sb.append(obj.toString());
        }
        return sb.toString();
      }
    };
  }
}

使用Redi緩存注解

service:

@CachePut(value = "user",key = "'user_'+#result.id")
  public UserPO save(UserPO po) {
    userJpaMapper.save(po);
    return po;
  }
 
  @CachePut(value = "user",key = "'user_'+#result.id")
  public UserPO update(UserPO po) {
    System.out.println("數(shù)據(jù)庫更新");
    userMapper.update(po);
    return po;
  }
 
  @Cacheable(value = "user", key = "'user_'+#id")
  public UserPO getUser(Integer id){
    System.out.println("訪問數(shù)據(jù)庫:"+id);
    return userJpaMapper.getOne(id);
  }
 
  @CacheEvict(value = "user", key = "'user_'+#id")
  public void delete(Integer id) {
    userJpaMapper.deleteById(id);
  }

測(cè)試類:

@Test
 void contextLoads() {
   Integer id = 2;
   UserPO user1 = userService.getUser(id);
   System.out.println("第一次查詢:"+user1.getUserName());
 
   UserPO user2 = userService.getUser(id);
   System.out.println("第二次查詢:"+user2.getUserName());
 }

測(cè)試結(jié)果:第一次查詢的時(shí)候訪問了數(shù)據(jù)庫,第二次查詢的時(shí)候并沒有訪問數(shù)據(jù)庫

通過redis-cli 可以查看到數(shù)據(jù)已經(jīng)保存到了redis上面

測(cè)試類:

@Test
 void updataUser() {
   Integer id = 4;
   UserPO user1 = userService.getUser(id);
   System.out.println("第一次查詢:"+user1.getUserName()+", 年齡:"+user1.getAge());
 
   user1.setAge(60);
   userService.update(user1);
 
   UserPO user2 = userService.getUser(id);
   System.out.println("第二次查詢:"+user2.getUserName()+", 年齡:"+user2.getAge());
 }

測(cè)試結(jié)果:

Redis工具類(redisUtil.java)

1.在RedisConfig中定義redisTemplate操作對(duì)象

/**
   * 對(duì)hash類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForHash();
  }
 
  /**
   * 對(duì)redis字符串類型數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForValue();
  }
 
  /**
   * 對(duì)鏈表類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForList();
  }
 
  /**
   * 對(duì)無序集合類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForSet();
  }
 
  /**
   * 對(duì)有序集合類型的數(shù)據(jù)操作
   * @param redisTemplate
   * @return
   */
  @Bean
  public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
    return redisTemplate.opsForZSet();
  }

2.在redisUtil工具類中使用這些對(duì)象,并構(gòu)建其操作方法

package com.meng.demo.utils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
 
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
 
 
/**
 * redis工具類
 */
@Component
public class RedisUtil {
  @Autowired
  private RedisTemplate<String, Object> redisTemplate;
  @Autowired
  private ValueOperations<String, Object> valueOperations;
  @Autowired
  private HashOperations<String, String, Object> hashOperations;
  @Autowired
  private ListOperations<String, Object> listOperations;
  @Autowired
  private SetOperations<String, Object> setOperations;
  @Autowired
  private ZSetOperations<String, Object> zSetOperations;
 
 
  /**=============================共同操作============================*/
  /**
   * 指定緩存失效時(shí)間
   * @param key 鍵
   * @param time 時(shí)間(秒)
   * @return
   */
  public boolean expire(String key,long time){
    try {
      if(time>0){
        redisTemplate.expire(key, time, TimeUnit.SECONDS);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 根據(jù)key 獲取過期時(shí)間
   * @param key 鍵不能為null
   * @return 時(shí)間(秒) 返回0代表為永久有效
   */
  public long getExpire(String key){
    return redisTemplate.getExpire(key,TimeUnit.SECONDS);
  }
  /**
   * 判斷key是否存在
   * @param key 鍵
   * @return true-存在、false-不存在
   */
  public boolean hasKey(String key){
    try {
      return redisTemplate.hasKey(key);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 刪除緩存
   * @param key 可以傳一個(gè)值或多個(gè)
   */
  public void del(String ... key){
    if(key!=null&&key.length>0){
      if(key.length==1){
        redisTemplate.delete(key[0]);
      }else{
        redisTemplate.delete(CollectionUtils.arrayToList(key));
      }
    }
  }
  /**============================ValueOperations操作=============================*/
  /**
   * 普通緩存放入
   * @param key 鍵
   * @param value 值
   * @return true-成功、 false-失敗
   */
  public boolean set(String key,Object value) {
    try {
      valueOperations.set(key, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 普通緩存放入并設(shè)置時(shí)間
   * @param key 鍵
   * @param value 值
   * @param expireTime 時(shí)間(秒) expireTime要大于0 如果expireTime小于等于0 將設(shè)置無限期
   * @return true成功 false失敗
   */
  public boolean set(String key,Object value,long expireTime){
    try {
      if(expireTime > 0){
        valueOperations.set(key, value, expireTime, TimeUnit.SECONDS);
      }else{
        set(key, value);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 普通緩存獲取
   * @param key 鍵
   * @return 值
   */
  public Object get(String key){
    return key==null ? null:valueOperations.get(key);
  }
  /**
   * 遞增
   * @param key 鍵
   * @return
   */
  public long incr(String key, long delta){
    if(delta<0){
      throw new RuntimeException("遞增因子必須大于0");
    }
    return valueOperations.increment(key, delta);
  }
  /**
   * 遞減
   * @param key
   * @param delta 要減少幾(小于0)
   * @return
   */
  public long decr(String key, long delta){
    if(delta<0){
      throw new RuntimeException("遞減因子必須大于0");
    }
    return valueOperations.increment(key, -delta);
  }
 
  /**================================HashOperations操作=================================*/
  /**
   * HashGet
   * @param key 鍵 不能為null
   * @param item 項(xiàng) 不能為null
   * @return 值
   */
  public Object hget(String key,String item){
    return hashOperations.get(key, item);
  }
 
  /**
   * 獲取hashKey對(duì)應(yīng)的所有鍵值
   * @param key 鍵
   * @return 對(duì)應(yīng)的多個(gè)鍵值
   */
  public Map<String, Object> hmget(String key){
    return hashOperations.entries(key);
  }
 
  /**
   * HashSet
   * @param key 鍵
   * @param map 對(duì)應(yīng)多個(gè)鍵值
   * @return true 成功 false 失敗
   */
  public boolean hmset(String key, Map<String,Object> map){
    try {
      hashOperations.putAll(key, map);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * HashSet 并設(shè)置時(shí)間
   * @param key 鍵
   * @param map 對(duì)應(yīng)多個(gè)鍵值
   * @param time 時(shí)間(秒)
   * @return true成功 false失敗
   */
  public boolean hmset(String key, Map<String,Object> map, long time){
    try {
      hashOperations.putAll(key, map);
      if(time>0){
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
   * @param key 鍵
   * @param item 項(xiàng)
   * @param value 值
   * @return true 成功 false失敗
   */
  public boolean hset(String key,String item,Object value) {
    try {
      hashOperations.put(key, item, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
   * @param key 鍵
   * @param item 項(xiàng)
   * @param value 值
   * @param time 時(shí)間(秒) 注意:如果已存在的hash表有時(shí)間,這里將會(huì)替換原有的時(shí)間
   * @return true 成功 false失敗
   */
  public boolean hset(String key,String item,Object value,long time) {
    try {
      hashOperations.put(key, item, value);
      if(time>0){
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 刪除hash表中的值
   * @param key 鍵 不能為null
   * @param item 項(xiàng) 可以使多個(gè) 不能為null
   */
  public void hdel(String key, Object... item){
    hashOperations.delete(key,item);
  }
 
  /**
   * 判斷hash表中是否有該項(xiàng)的值
   * @param key 鍵 不能為null
   * @param item 項(xiàng) 不能為null
   * @return true 存在 false不存在
   */
  public boolean hHasKey(String key, String item){
    return hashOperations.hasKey(key, item);
  }
 
  /**
   * hash遞增 如果不存在,就會(huì)創(chuàng)建一個(gè) 并把新增后的值返回
   * @param key 鍵
   * @param item 項(xiàng)
   * @param by 要增加幾(大于0)
   * @return
   */
  public double hincr(String key, String item,double by){
    return hashOperations.increment(key, item, by);
  }
 
  /**
   * hash遞減
   * @param key 鍵
   * @param item 項(xiàng)
   * @param by 要減少記(小于0)
   * @return
   */
  public double hdecr(String key, String item,double by){
    return hashOperations.increment(key, item,-by);
  }
 
  /**============================set=============================
   /**
   * 根據(jù)key獲取Set中的所有值
   * @param key 鍵
   * @return
   */
  public Set<Object> sGet(String key){
    try {
      return setOperations.members(key);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
 
  /**
   * 根據(jù)value從一個(gè)set中查詢,是否存在
   * @param key 鍵
   * @param value 值
   * @return true 存在 false不存在
   */
  public boolean sHasKey(String key,Object value){
    try {
      return setOperations.isMember(key, value);
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 將數(shù)據(jù)放入set緩存
   * @param key 鍵
   * @param values 值 可以是多個(gè)
   * @return 成功個(gè)數(shù)
   */
  public long sSet(String key, Object...values) {
    try {
      return setOperations.add(key, values);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
 
  /**
   * 將set數(shù)據(jù)放入緩存
   * @param key 鍵
   * @param time 時(shí)間(秒)
   * @param values 值 可以是多個(gè)
   * @return 成功個(gè)數(shù)
   */
  public long sSetAndTime(String key,long time,Object...values) {
    try {
      Long count = setOperations.add(key, values);
      if(time>0){
        expire(key, time);
      }
      return count;
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
 
  /**
   * 獲取set緩存的長度
   * @param key 鍵
   * @return
   */
  public long sGetSetSize(String key){
    try {
      return setOperations.size(key);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
 
  /**
   * 移除值為value的
   * @param key 鍵
   * @param values 值 可以是多個(gè)
   * @return 移除的個(gè)數(shù)
   */
  public long setRemove(String key, Object ...values) {
    try {
      Long count = setOperations.remove(key, values);
      return count;
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
  /**===============================ListOperations=================================
   /**
   * 獲取list緩存的內(nèi)容
   * @param key 鍵
   * @param start 開始
   * @param end 結(jié)束 0 到 -1代表所有值
   * @return
   */
  public List<Object> lGet(String key, long start, long end){
    try {
      return listOperations.range(key, start, end);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  /**
   * 獲取list緩存的所有內(nèi)容
   * @param key
   * @return
   */
  public List<Object> lGetAll(String key){
    return lGet(key,0,-1);
  }
 
  /**
   * 獲取list緩存的長度
   * @param key 鍵
   * @return
   */
  public long lGetListSize(String key){
    try {
      return listOperations.size(key);
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
  /**
   * 通過索引 獲取list中的值
   * @param key 鍵
   * @param index 索引 index>=0時(shí), 0 表頭,1 第二個(gè)元素,依次類推;index<0時(shí),-1,表尾,-2倒數(shù)第二個(gè)元素,依次類推
   * @return
   */
  public Object lGetIndex(String key,long index){
    try {
      return listOperations.index(key, index);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @return
   */
  public boolean lSet(String key, Object value) {
    try {
      listOperations.rightPush(key, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @param time 時(shí)間(秒)
   * @return
   */
  public boolean lSet(String key, Object value, long time) {
    try {
      listOperations.rightPush(key, value);
      if (time > 0){
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @return
   */
  public boolean lSet(String key, List<Object> value) {
    try {
      listOperations.rightPushAll(key, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 將list放入緩存
   * @param key 鍵
   * @param value 值
   * @param time 時(shí)間(秒)
   * @return
   */
  public boolean lSet(String key, List<Object> value, long time) {
    try {
      listOperations.rightPushAll(key, value);
      if (time > 0) {
        expire(key, time);
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  /**
   * 根據(jù)索引修改list中的某條數(shù)據(jù)
   * @param key 鍵
   * @param index 索引
   * @param value 值
   * @return
   */
  public boolean lUpdateIndex(String key, long index,Object value) {
    try {
      listOperations.set(key, index, value);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
 
  /**
   * 移除N個(gè)值為value
   * @param key 鍵
   * @param count 移除多少個(gè)
   * @param value 值
   * @return 移除的個(gè)數(shù)
   */
  public long lRemove(String key,long count,Object value) {
    try {
      Long remove = listOperations.remove(key, count, value);
      return remove;
    } catch (Exception e) {
      e.printStackTrace();
      return 0;
    }
  }
}

3.測(cè)試

@Test
 void test01(){
   redisUtil.set("meng","yang");
   Object key = redisUtil.get("meng");
   System.out.println(key);
 }

 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 一文詳解Java中字符串的基本操作

    一文詳解Java中字符串的基本操作

    這篇文章主要為大家詳細(xì)介紹了Java中字符串的基本操作,例如遍歷、統(tǒng)計(jì)次數(shù),拼接和反轉(zhuǎn)等以及String的常用方法,感興趣的可以了解一下
    2022-08-08
  • SpringBoot多種自定義錯(cuò)誤頁面方式小結(jié)

    SpringBoot多種自定義錯(cuò)誤頁面方式小結(jié)

    這篇文章主要介紹了SpringBoot多種自定義錯(cuò)誤頁面方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java獲取http和https協(xié)議返回的json數(shù)據(jù)

    Java獲取http和https協(xié)議返回的json數(shù)據(jù)

    本篇文章主要介紹了Java獲取http和https協(xié)議返回的json數(shù)據(jù) ,本篇文章提供兩個(gè)方法,幫助各位如何獲取http和https返回的數(shù)據(jù)。有興趣的可以了解一下。
    2017-01-01
  • Java實(shí)現(xiàn)線程的暫停和恢復(fù)的示例詳解

    Java實(shí)現(xiàn)線程的暫停和恢復(fù)的示例詳解

    這幾天的項(xiàng)目中,客戶給了個(gè)需求,希望我可以開啟一個(gè)任務(wù),想什么時(shí)候暫停就什么時(shí)候暫停,想什么時(shí)候開始就什么時(shí)候開始,所以本文小編給大家介紹了Java實(shí)現(xiàn)線程的暫停和恢復(fù)的示例,需要的朋友可以參考下
    2023-11-11
  • Java矩陣連乘問題(動(dòng)態(tài)規(guī)劃)算法實(shí)例分析

    Java矩陣連乘問題(動(dòng)態(tài)規(guī)劃)算法實(shí)例分析

    這篇文章主要介紹了Java矩陣連乘問題(動(dòng)態(tài)規(guī)劃)算法,結(jié)合實(shí)例形式分析了java實(shí)現(xiàn)矩陣連乘的算法原理與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-11-11
  • IDEA的下載和使用安裝詳細(xì)圖文教程

    IDEA的下載和使用安裝詳細(xì)圖文教程

    這篇文章主要介紹了IDEA的下載和使用安裝,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 詳解java構(gòu)建者模式Builder

    詳解java構(gòu)建者模式Builder

    這篇文章主要介紹了java構(gòu)建者模式Builder,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Java?EasyExcel導(dǎo)出合并單元格的示例詳解

    Java?EasyExcel導(dǎo)出合并單元格的示例詳解

    EasyExcel是阿里巴巴開源的一個(gè)excel處理框架,以使用簡單、節(jié)省內(nèi)存著稱,這篇文章主要為大家介紹了如何利用EasyExcel導(dǎo)出合并單元格,需要的可以參考下
    2023-09-09
  • Jenkins Host key verification failed問題解決

    Jenkins Host key verification failed問題解決

    這篇文章主要介紹了Jenkins Host key verification failed問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • JVM入門之內(nèi)存結(jié)構(gòu)(堆、方法區(qū))

    JVM入門之內(nèi)存結(jié)構(gòu)(堆、方法區(qū))

    JVM 基本上是每家招聘公司都會(huì)問到的問題,它們會(huì)這么無聊問這些不切實(shí)際的問題嗎?很顯然不是。由 JVM 引發(fā)的故障問題,無論在我們開發(fā)過程中還是生產(chǎn)環(huán)境下都是非常常見的
    2021-06-06

最新評(píng)論