銆愯浆銆?springboot鏁村悎redis-sentinel鏀寔Cache娉ㄨВ

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愯浆銆?springboot鏁村悎redis-sentinel鏀寔Cache娉ㄨВ相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/util' title='util'>util   澹版槑   find   鏃堕棿   hang   void   database   @param   long   

銆愯浆銆?springboot鏁村悎redis-sentinel鏀寔Cache娉ㄨВ

涓€銆佸墠鎻?/span>

宸茬粡瀛樺湪涓€涓猺edis-sentinel闆嗙兢锛屼袱涓摠鍏靛垎鍒涓嬶細

/home/redis-sentinel-cluster/sentinel-1.conf
鎶€鏈浘鐗? src=
port 26379

dir "/data"
sentinel monitor mymaster 172.16.1.11 16379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1
鎶€鏈浘鐗? src=
/home/redis-sentinel-cluster/sentinel-2.conf
鎶€鏈浘鐗? src=
port 26380

dir "/data"
sentinel monitor mymaster 172.16.1.11 16379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 5000
sentinel parallel-syncs mymaster 1
鎶€鏈浘鐗? src=

 

浜屻€佹柊寤簃aven宸ョ▼锛?strong>redis-sentinel-demo    鏈€缁堝畬鏁村伐绋嬪涓嬶細

鎶€鏈浘鐗? src=

 

pom.xml濡備笅锛?/p>

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.redis.sentinel.demo</groupId>
    <artifactId>redis-sentinel-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--浠ヤ笅鏄痵pring鏁村悎redis鐨勪緷璧?/span>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>

</project>

1銆乤pplication.yml

server:
  port: 8083
  context-path: /

spring:
  redis:
    sentinel:
      master: mymaster
      nodes: 172.16.1.11:26379,172.16.1.11:26380
    pool:
      max-active: 8
      max-idle: 8
      max-wait: -1
      min-idle: 0
    database: 0

2銆佹柊寤簉edis鐨勫伐鍏风被RedisUtil

package com.redis.sentinel.demo.util;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 * @date 2019/03/19
 */
public class RedisUtil {

    private RedisTemplate<String, Object> redisTemplate;
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }
    //=============================common============================
    /**
     * 鎸囧畾缂撳瓨澶辨晥鏃堕棿
     * @param key 閿?     * @param time 鏃堕棿(绉?
     * @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;
        }
    }

    /**
     * 鏍规嵁key 鑾峰彇杩囨湡鏃堕棿
     * @param key 閿?涓嶈兘涓簄ull
     * @return 鏃堕棿(绉? 杩斿洖0浠h〃涓烘案涔呮湁鏁?     */
    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 鍙互浼犱竴涓€?鎴栧涓?     */
    @SuppressWarnings("unchecked")
    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));
            }
        }
    }

    //============================String=============================
    /**
     * 鏅€氱紦瀛樿幏鍙?     * @param key 閿?     * @return 鍊?     */
    public Object get(String key){
        return key==null?null:redisTemplate.opsForValue().get(key);
    }

    /**
     * 鏅€氱紦瀛樻斁鍏?     * @param key 閿?     * @param value 鍊?     * @return true鎴愬姛 false澶辫触
     */
    public boolean set(String key,Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 鏅€氱紦瀛樻斁鍏ュ苟璁剧疆鏃堕棿
     * @param key 閿?     * @param value 鍊?     * @param time 鏃堕棿(绉? time瑕佸ぇ浜? 濡傛灉time灏忎簬绛変簬0 灏嗚缃棤闄愭湡
     * @return true鎴愬姛 false 澶辫触
     */
    public boolean set(String key,Object value,long time){
        try {
            if(time>0){
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            }else{
                set(key, value);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 閫掑
     * @param key 閿?     * @param delta 瑕佸鍔犲嚑(澶т簬0)
     * @return
     */
    public long incr(String key, long delta){
        if(delta<0){
            throw new RuntimeException("閫掑鍥犲瓙蹇呴』澶т簬0");
        }
        return redisTemplate.opsForValue().increment(key, delta);
    }

    /**
     * 閫掑噺
     * @param key 閿?     * @param delta 瑕佸噺灏戝嚑(灏忎簬0)
     * @return
     */
    public long decr(String key, long delta){
        if(delta<0){
            throw new RuntimeException("閫掑噺鍥犲瓙蹇呴』澶т簬0");
        }
        return redisTemplate.opsForValue().increment(key, -delta);
    }

    //================================Hash=================================
    /**
     * HashGet
     * @param key 閿?涓嶈兘涓簄ull
     * @param item 椤?涓嶈兘涓簄ull
     * @return 鍊?     */
    public Object hget(String key,String item){
        return redisTemplate.opsForHash().get(key, item);
    }

    /**
     * 鑾峰彇hashKey瀵瑰簲鐨勬墍鏈夊€?     * @param key 閿?     * @return 瀵瑰簲鐨勫涓€?     */
    public Map<Object,Object> hmget(String key){
        return redisTemplate.opsForHash().entries(key);
    }

    /**
     * HashSet
     * @param key 閿?     * @param map 瀵瑰簲澶氫釜閿€?     * @return true 鎴愬姛 false 澶辫触
     */
    public boolean hmset(String key, Map<String,Object> map){
        try {
            redisTemplate.opsForHash().putAll(key, map);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * HashSet 骞惰缃椂闂?     * @param key 閿?     * @param map 瀵瑰簲澶氫釜閿€?     * @param time 鏃堕棿(绉?
     * @return true鎴愬姛 false澶辫触
     */
    public boolean hmset(String key, Map<String,Object> map, long time){
        try {
            redisTemplate.opsForHash().putAll(key, map);
            if(time>0){
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 鍚戜竴寮爃ash琛ㄤ腑鏀惧叆鏁版嵁,濡傛灉涓嶅瓨鍦ㄥ皢鍒涘缓
     * @param key 閿?     * @param item 椤?     * @param value 鍊?     * @return true 鎴愬姛 false澶辫触
     */
    public boolean hset(String key,String item,Object value) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 鍚戜竴寮爃ash琛ㄤ腑鏀惧叆鏁版嵁,濡傛灉涓嶅瓨鍦ㄥ皢鍒涘缓
     * @param key 閿?     * @param item 椤?     * @param value 鍊?     * @param time 鏃堕棿(绉?  娉ㄦ剰:濡傛灉宸插瓨鍦ㄧ殑hash琛ㄦ湁鏃堕棿,杩欓噷灏嗕細鏇挎崲鍘熸湁鐨勬椂闂?     * @return true 鎴愬姛 false澶辫触
     */
    public boolean hset(String key,String item,Object value,long time) {
        try {
            redisTemplate.opsForHash().put(key, item, value);
            if(time>0){
                expire(key, time);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 鍒犻櫎hash琛ㄤ腑鐨勫€?     * @param key 閿?涓嶈兘涓簄ull
     * @param item 椤?鍙互浣垮涓?涓嶈兘涓簄ull
     */
    public void hdel(String key, Object... item){
        redisTemplate.opsForHash().delete(key,item);
    }

    /**
     * 鍒ゆ柇hash琛ㄤ腑鏄惁鏈夎椤圭殑鍊?     * @param key 閿?涓嶈兘涓簄ull
     * @param item 椤?涓嶈兘涓簄ull
     * @return true 瀛樺湪 false涓嶅瓨鍦?     */
    public boolean hHasKey(String key, String item){
        return redisTemplate.opsForHash().hasKey(key, item);
    }

    /**
     * hash閫掑 濡傛灉涓嶅瓨鍦?灏变細鍒涘缓涓€涓?骞舵妸鏂板鍚庣殑鍊艰繑鍥?     * @param key 閿?     * @param item 椤?     * @param by 瑕佸鍔犲嚑(澶т簬0)
     * @return
     */
    public double hincr(String key, String item,double by){
        return redisTemplate.opsForHash().increment(key, item, by);
    }

    /**
     * hash閫掑噺
     * @param key 閿?     * @param item 椤?     * @param by 瑕佸噺灏戣(灏忎簬0)
     * @return
     */
    public double hdecr(String key, String item,double by){
        return redisTemplate.opsForHash().increment(key, item,-by);
    }

    //============================set=============================
    /**
     * 鏍规嵁key鑾峰彇Set涓殑鎵€鏈夊€?     * @param key 閿?     * @return
     */
    public Set<Object> sGet(String key){
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 鏍规嵁value浠庝竴涓猻et涓煡璇?鏄惁瀛樺湪
     * @param key 閿?     * @param value 鍊?     * @return true 瀛樺湪 false涓嶅瓨鍦?     */
    public boolean sHasKey(String key,Object value){
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 灏嗘暟鎹斁鍏et缂撳瓨
     * @param key 閿?     * @param values 鍊?鍙互鏄涓?     * @return 鎴愬姛涓暟
     */
    public long sSet(String key, Object...values) {
        try {
            return redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 灏唖et鏁版嵁鏀惧叆缂撳瓨
     * @param key 閿?     * @param time 鏃堕棿(绉?
     * @param values 鍊?鍙互鏄涓?     * @return 鎴愬姛涓暟
     */
    public long sSetAndTime(String key,long time,Object...values) {
        try {
            Long count = redisTemplate.opsForSet().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 redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 绉婚櫎鍊间负value鐨?     * @param key 閿?     * @param values 鍊?鍙互鏄涓?     * @return 绉婚櫎鐨勪釜鏁?     */
    public long setRemove(String key, Object ...values) {
        try {
            Long count = redisTemplate.opsForSet().remove(key, values);
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    //===============================list=================================

    /**
     * 鑾峰彇list缂撳瓨鐨勫唴瀹?     * @param key 閿?     * @param start 寮€濮?     * @param end 缁撴潫  0 鍒?-1浠h〃鎵€鏈夊€?     * @return
     */
    public List<Object> lGet(String key, long start, long end){
        try {
            return redisTemplate.opsForList().range(key, start, end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 鑾峰彇list缂撳瓨鐨勯暱搴?     * @param key 閿?     * @return
     */
    public long lGetListSize(String key){
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 閫氳繃绱㈠紩 鑾峰彇list涓殑鍊?     * @param key 閿?     * @param index 绱㈠紩  index>=0鏃讹紝 0 琛ㄥご锛? 绗簩涓厓绱狅紝渚濇绫绘帹锛沬ndex<0鏃讹紝-1锛岃〃灏撅紝-2鍊掓暟绗簩涓厓绱狅紝渚濇绫绘帹
     * @return
     */
    public Object lGetIndex(String key,long index){
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 灏唋ist鏀惧叆缂撳瓨
     * @param key 閿?     * @param value 鍊?     * @return
     */
    public boolean lSet(String key, Object value) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 灏唋ist鏀惧叆缂撳瓨
     * @param key 閿?     * @param value 鍊?     * @param time 鏃堕棿(绉?
     * @return
     */
    public boolean lSet(String key, Object value, long time) {
        try {
            redisTemplate.opsForList().rightPush(key, value);
            if (time > 0) expire(key, time);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 灏唋ist鏀惧叆缂撳瓨
     * @param key 閿?     * @param value 鍊?     * @return
     */
    public boolean lSet(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 灏唋ist鏀惧叆缂撳瓨
     * @param key 閿?     * @param value 鍊?     * @param time 鏃堕棿(绉?
     * @return
     */
    public boolean lSet(String key, List<Object> value, long time) {
        try {
            redisTemplate.opsForList().rightPushAll(key, value);
            if (time > 0) expire(key, time);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 鏍规嵁绱㈠紩淇敼list涓殑鏌愭潯鏁版嵁
     * @param key 閿?     * @param index 绱㈠紩
     * @param value 鍊?     * @return
     */
    public boolean lUpdateIndex(String key, long index,Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 鐢ㄤ簬绉婚櫎閿腑鎸囧畾鐨勫厓绱犮€傛帴鍙?涓弬鏁帮紝鍒嗗埆鏄紦瀛樼殑閿悕锛岃鏁颁簨浠讹紝瑕佺Щ闄ょ殑鍊笺€傝鏁颁簨浠跺彲浠ヤ紶鍏ョ殑鏈変笁涓€硷紝鍒嗗埆鏄?1銆?銆?銆?     -1浠h〃浠庡瓨鍌ㄥ鍣ㄧ殑鏈€鍙宠竟寮€濮嬶紝鍒犻櫎涓€涓笌瑕佺Щ闄ょ殑鍊煎尮閰嶇殑鏁版嵁锛?浠h〃鍒犻櫎鎵€鏈変笌浼犲叆鍊煎尮閰嶇殑鏁版嵁锛?浠h〃浠庡瓨鍌ㄥ鍣ㄧ殑鏈€宸﹁竟寮€濮嬶紝鍒犻櫎涓€涓笌瑕佺Щ闄ょ殑鍊煎尮閰嶇殑鏁版嵁銆?     * @param key 閿?     * @param count
     * @param value 鍊?     * @return 绉婚櫎鐨勪釜鏁?     */
    public void lRemove(String key,long count,Object value) {
        try {
            redisTemplate.opsForList().remove(key, count, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3銆佹柊寤篟edisConfig锛堜富瑕佹槸璁剧疆瀹炰緥鍖杛edisTemplate锛屽苟璁剧疆搴忓垪鍖栵紝鍚屾椂瀹炰緥鍖朢edisUtil锛?/p>

package com.redis.sentinel.demo.config;

import com.redis.sentinel.demo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;



/**
 * @author Administrator
 * @date 2019/03/19
 */
@Configuration
@EnableCaching  //寮€鍚紦瀛?杩樿缁ф壙浜嶤achingConfigurerSupport锛屼富瑕佹槸涓轰簡娉ㄨВ@Cacheable銆丂CacheEvict銆丂CachePut绛夌殑浣跨敤
public class RedisConfig extends CachingConfigurerSupport {
    /**
     * 娉ㄥ叆 RedisConnectionFactory
     */
    @Autowired
    RedisConnectionFactory redisConnectionFactory;

    /**
     * 瀹炰緥鍖?RedisTemplate 瀵硅薄
     *
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> functionDomainRedisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        initDomainRedisTemplate(redisTemplate, redisConnectionFactory);
        return redisTemplate;
    }

    /**
     * 璁剧疆鏁版嵁瀛樺叆 redis 鐨勫簭鍒楀寲鏂瑰紡
     *
     * @param redisTemplate
     * @param factory
     */
    private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //鍙湁璁剧疆jdk搴忓垪鍖栵紝鎵嶈兘鏂扮被瀵硅薄姣斿User杩涜瀛樺偍
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
    }

    /**
     * 瀹炰緥鍖朢edisUtil
     * @param redisTemplate
     * @return
     */
    @Bean
    public RedisUtil redisUtil(RedisTemplate<String, Object> redisTemplate) {
        RedisUtil redisUtil = new RedisUtil();
        redisUtil.setRedisTemplate(redisTemplate);
        return redisUtil;
    }
}

4銆佸垱寤篟edisTestController 

package com.redis.sentinel.demo.controller;

import com.redis.sentinel.demo.model.User;
import com.redis.sentinel.demo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Administrator
 * @date 2019/03/19
 */
@RestController
@RequestMapping("/redis-sentinel")
public class RedisTestController {
    @Autowired
    private RedisUtil redisUtil;
    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public void postTest(){
        testCommon();
        testHash();
        testSet();
        testList();
    }

    private void testCommon(){
        System.out.println("================================娴嬭瘯鏅€氱紦瀛?=================");
        System.out.println("鏅€氱紦瀛?瀛樺叆 key01 鍊间负value01 鍒版湡鏃堕棿涓?绉?);
        redisUtil.set("key01","value01",5);
        System.out.println("浠巖edis鑾峰彇key01鐨勫€硷細"+redisUtil.get("key01"));
        System.out.println("鍒版湡鏃堕棿涓猴細"+redisUtil.getExpire("key01"));
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("6绉掑悗浠巖edis鑾峰彇key01鐨勫€硷細"+redisUtil.get("key01"));
        System.out.println("key01鏄惁瀛樺湪:"+redisUtil.hasKey("key01"));
    }

    private void testHash(){
        System.out.println("================================娴嬭瘯Hash缂撳瓨==================");
        System.out.println("hash缂撳瓨,瀛樺叆 key03 鍊间负{"name":"zhangsan","sex":"man"}");
        Map<String,Object> map = new HashMap<>();
        map.put("name","zhangsan");
        map.put("sex","man");
        redisUtil.hmset("key03",map);
        System.out.println("key03:"+redisUtil.hget("key03","name")+"  "+redisUtil.hget("key03","sex"));
        redisUtil.del("key03");
    }

    private void testSet(){
        System.out.println("================================娴嬭瘯Set缂撳瓨==================");
        System.out.println("Set缂撳瓨,灏嗕袱涓猆ser鏀惧叆缂撳瓨key04");
        redisUtil.sSet("key04",new User("name1","man"),new User("name2","femal"));
        Set<Object> users = redisUtil.sGet("key04");
        for(Object o:users){
            User user = (User)o;
            System.out.println(o.toString());
        }
        System.out.println("鑾峰彇Set key04鐨勯暱搴︼細"+redisUtil.sGetSetSize("key04"));
        System.out.println("鍒犻櫎key04");
        redisUtil.del("key04");
        System.out.println("鑾峰彇Set key04鐨勯暱搴︼細"+redisUtil.sGetSetSize("key04"));

    }

    private void testList(){
        System.out.println("================================娴嬭瘯List缂撳瓨==================");
        System.out.println("List缂撳瓨key05");
        redisUtil.lSet("key05", Arrays.asList("aa","bb","cc","dd","ee","ff","gg"));
        System.out.println("List缂撳瓨key06");
        redisUtil.lSet("key06","11");
        redisUtil.lSet("key06","22");
        redisUtil.lSet("key06","33");
        redisUtil.lSet("key06","44");
        redisUtil.lSet("key06","55");
        redisUtil.lSet("key06","66");
        redisUtil.lSet("key06","77");
        System.out.println("浠ヤ笂涓ょ鏂瑰紡鐨勭紦瀛樻槸鏈夊尯鍒殑锛屾敞鎰忕湅涓嬮潰鐨勯暱搴?);
        System.out.println("杈撳嚭key05鐨勯暱搴︼細"+redisUtil.lGetListSize("key05"));
        List<Object> list = redisUtil.lGet("key05",0,redisUtil.lGetListSize("key05"));
        System.out.println("杈撳嚭key05鐨勬墍鏈夊厓绱?);
        for(Object str:list){
            System.out.println(str);
        }
        System.out.println("杈撳嚭key06鐨勯暱搴︼細"+redisUtil.lGetListSize("key06"));
        List<Object> list1 = redisUtil.lGet("key06",0,redisUtil.lGetListSize("key06"));
        System.out.println("杈撳嚭key06鐨勬墍鏈夊厓绱?);
        for(Object str:list1){
            System.out.println(str);
        }

        System.out.println("鍒犻櫎key06鐨勭殑55");
        redisUtil.lRemove("key06",1,"55");
        List<Object> list2 = redisUtil.lGet("key06",0,redisUtil.lGetListSize("key06"));
        System.out.println("杈撳嚭key06鐨勯暱搴︼細"+redisUtil.lGetListSize("key06"));
        System.out.println("杈撳嚭key06鐨勬墍鏈夊厓绱?);
        for(Object str:list2){
            System.out.println(str);
        }

        redisUtil.del("key06");
        redisUtil.del("key05");

    }
}

5銆佸垱寤簊pringboot鍚姩绫?/p>

package com.redis.sentinel.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author Administrator
 * @date 2019/03/19
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args){
        SpringApplication.run(Application.class, args);
    }
}

6銆佸惎鍔ㄧ▼搴忓苟鐢╬ostman杩涜娴嬭瘯

鎶€鏈浘鐗? src=

================================娴嬭瘯鏅€氱紦瀛?=================
鏅€氱紦瀛?瀛樺叆 key01 鍊间负value01 鍒版湡鏃堕棿涓?绉?浠巖edis鑾峰彇key01鐨勫€硷細value01
鍒版湡鏃堕棿涓猴細4
6绉掑悗浠巖edis鑾峰彇key01鐨勫€硷細null
key01鏄惁瀛樺湪:false
================================娴嬭瘯Hash缂撳瓨==================
hash缂撳瓨,瀛樺叆 key03 鍊间负{"name":"zhangsan","sex":"man"}
key03:zhangsan  man
================================娴嬭瘯Set缂撳瓨==================
Set缂撳瓨,灏嗕袱涓猆ser鏀惧叆缂撳瓨key04
User{name=鈥?/span>name2鈥?/span>, sex=鈥?/span>femal鈥?/span>}
User{name=鈥?/span>name1鈥?/span>, sex=鈥?/span>man鈥?/span>}
鑾峰彇Set key04鐨勯暱搴︼細2
鍒犻櫎key04
鑾峰彇Set key04鐨勯暱搴︼細0
================================娴嬭瘯List缂撳瓨==================
List缂撳瓨key05
List缂撳瓨key06
浠ヤ笂涓ょ鏂瑰紡鐨勭紦瀛樻槸鏈夊尯鍒殑锛屾敞鎰忕湅涓嬮潰鐨勯暱搴?杈撳嚭key05鐨勯暱搴︼細1
杈撳嚭key05鐨勬墍鏈夊厓绱?[aa, bb, cc, dd, ee, ff, gg]
杈撳嚭key06鐨勯暱搴︼細7
杈撳嚭key06鐨勬墍鏈夊厓绱?11
22
33
44
55
66
77
鍒犻櫎key06鐨勭殑55
杈撳嚭key06鐨勯暱搴︼細6
杈撳嚭key06鐨勬墍鏈夊厓绱?11
22
33
44
66
77

 

涓夈€佷娇鐢–ache娉ㄨВ

1銆丂Cacheable

@Cacheable鏄敤鏉ュ0鏄庢柟娉曟槸鍙紦瀛樼殑銆傚皢缁撴灉瀛樺偍鍒扮紦瀛樹腑浠ヤ究鍚庣画浣跨敤鐩稿悓鍙傛暟璋冪敤鏃朵笉闇€鎵ц瀹為檯鐨勬柟娉曘€傜洿鎺ヤ粠缂撳瓨涓彇鍊笺€傛渶绠€鍗曠殑鏍煎紡闇€瑕佸埗瀹氱紦瀛樺悕绉般€備富瑕佺敤浜庢煡璇€?/p>

 鍙傛暟  瑙i噴  绀轰緥
 value  缂撳瓨鐨勫悕绉帮紝蹇呴』鎸囧畾鑷冲皯涓€涓?nbsp;   
@Cacheable(value=鈥漸sers鈥?
@Cacheable(value={鈥漸sers1鈥?鈥漸sers2鈥潁
 key 缂撳瓨鐨?key锛屽彲浠ヤ负绌猴紝濡傛灉鎸囧畾瑕佹寜鐓?SpEL 琛ㄨ揪寮忕紪鍐欙紝濡傛灉涓嶆寚瀹氾紝鍒欑己鐪佹寜鐓ф柟娉曠殑鎵€鏈夊弬鏁拌繘琛岀粍鍚?/td>  
@Cacheable(value = "users", key = "#name")
@Cacheable(value = "users", key = "#p0")
 condition  缂撳瓨鐨勬潯浠讹紝鍙互涓虹┖锛屼娇鐢?SpEL 缂栧啓锛岃繑鍥?true 鎴栬€?false锛屽彧鏈変负 true 鎵嶈繘琛岀紦瀛?/td>  
@Cacheable(value = "users",key = "#p0", condition = "#p0 != null")

 

 

 

 

 

 

 

 

 

 

 

 

 

2銆丂CachePut

濡傛灉缂撳瓨闇€瑕佹洿鏂帮紝涓斾笉骞叉壈鏂规硶鐨勬墽琛?鍙互浣跨敤娉ㄨВ@CachePut銆侤CachePut鏍囨敞鐨勬柟娉曞湪鎵ц鍓嶄笉浼氬幓妫€鏌ョ紦瀛樹腑鏄惁瀛樺湪涔嬪墠鎵ц杩囩殑缁撴灉锛岃€屾槸姣忔閮戒細鎵ц璇ユ柟娉曪紝骞跺皢鎵ц缁撴灉浠ラ敭鍊煎鐨勫舰寮忓瓨鍏ユ寚瀹氱殑缂撳瓨涓€?/p>

 鍙傛暟  瑙i噴  绀轰緥
 value  缂撳瓨鐨勫悕绉帮紝蹇呴』鎸囧畾鑷冲皯涓€涓?nbsp;   
@CachePut(value=鈥漸sers鈥?
@CachePut(value={鈥漸sers1鈥?鈥漸sers2鈥潁
 key 缂撳瓨鐨?key锛屽彲浠ヤ负绌猴紝濡傛灉鎸囧畾瑕佹寜鐓?SpEL 琛ㄨ揪寮忕紪鍐欙紝濡傛灉涓嶆寚瀹氾紝鍒欑己鐪佹寜鐓ф柟娉曠殑鎵€鏈夊弬鏁拌繘琛岀粍鍚?/td>  
@CachePut(value = "users", key = "#name")
@CachePut(value = "users", key = "#p0")
 condition  缂撳瓨鐨勬潯浠讹紝鍙互涓虹┖锛屼娇鐢?SpEL 缂栧啓锛岃繑鍥?true 鎴栬€?false锛屽彧鏈変负 true 鎵嶈繘琛岀紦瀛?/td>  
@CachePut(value = "users",key = "#p0", condition = "#p0 != null")

 

 

 

 

 

 

 

 

 

3銆丂CacheEvict

@CachEvict 鐨勪綔鐢?涓昏閽堝鏂规硶閰嶇疆锛岃兘澶熸牴鎹竴瀹氱殑鏉′欢瀵圭紦瀛樿繘琛屾竻绌?/p>

 鍙傛暟  瑙i噴  绀轰緥
 value  缂撳瓨鐨勫悕绉帮紝蹇呴』鎸囧畾鑷冲皯涓€涓?nbsp;   
@CacheEvict(value=鈥漸sers鈥?
 key 缂撳瓨鐨?key锛屽彲浠ヤ负绌猴紝濡傛灉鎸囧畾瑕佹寜鐓?SpEL 琛ㄨ揪寮忕紪鍐欙紝濡傛灉涓嶆寚瀹氾紝鍒欑己鐪佹寜鐓ф柟娉曠殑鎵€鏈夊弬鏁拌繘琛岀粍鍚?/td>  
@CacheEvict(value = "users", key = "#name")
@CacheEvict(value = "users", key = "#p0")
 condition  缂撳瓨鐨勬潯浠讹紝鍙互涓虹┖锛屼娇鐢?SpEL 缂栧啓锛岃繑鍥?true 鎴栬€?false锛屽彧鏈変负 true 鎵嶈繘琛岀紦瀛?/td>  
@CacheEvict(value = "users",key = "#p0", condition = "#p0 != null")
 allEntries 缂虹渷涓?false锛屽鏋滄寚瀹氫负 true锛屽垯鏂规硶璋冪敤鍚庡皢绔嬪嵆娓呯┖鎵€鏈夌紦瀛?/td>
@CacheEvict(value = "users",allEntries=true)
 beforeInvocation 缂虹渷涓?false锛屽鏋滄寚瀹氫负 true锛屽垯鍦ㄦ柟娉曡繕娌℃湁鎵ц鐨勬椂鍊欏氨娓呯┖缂撳瓨锛岀己鐪佹儏鍐典笅锛屽鏋滄柟娉曟墽琛屾姏鍑哄紓甯革紝鍒欎笉浼氭竻绌虹紦瀛?/td>  
@CacheEvict(value = "users",beforeInvocation=true)

 

 

 

 

 

 

 

 

 

 

 

 

4銆丂Caching

鏈夋椂鍊欐垜浠彲鑳界粍鍚堝涓狢ache娉ㄨВ浣跨敤

 

浠ヤ笂鍥涗釜涓敞瑙g殑绀轰緥绋嬪簭濡備笅锛?/p>

鍦ㄤ笂闈㈢殑绋嬩腑鏂板缓RedisCachableController 

package com.redis.sentinel.demo.controller;

import com.redis.sentinel.demo.model.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/03/21
 */
@RestController
@RequestMapping("/redis-sentinel-cache")
public class RedisCachableController {

    private Map<String, Object> userList = new HashMap<>();
    /**
     * 鎸塳ey涓簎ser.name杩涜缂撳瓨
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @CachePut(value = "users", key = "#user.name")
    public User addUser(@RequestBody User user) {
        userList.put(user.getName(), user);
        return user;
    }

    /**
     * 缁勫悎澶氫釜Cache娉ㄨВ浣跨敤,鎸塽ser.name鍜寀ser.sex涓や釜缁村害杩涜缂撳瓨
     * @param user
     * @return
     */
    @RequestMapping(value = "/add2", method = RequestMethod.POST)
    @Caching(put = {
            @CachePut(value = "users", key = "#user.name"),
            @CachePut(value = "users", key = "#user.sex")
    })
    public User addUser2(@RequestBody User user) {
        userList.put(user.getName(), user);
        return user;
    }


    /**
     * 鍏堜粠缂撳瓨鏌ユ暟鎹紝濡傛灉缂撳瓨娌℃湁锛屽垯鍒皍serList閲岄潰鍘绘嬁銆?     * @param name
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @Cacheable(value = "users", key = "#name", condition = "#name != null")
    public User queryUserByName(@RequestParam(value = "name") String name) {
        System.out.println("濡傛灉缂撳瓨娌℃湁锛屼粠map閲岄潰鑾峰彇");
        User user = (User) userList.get(name);
        return user;
    }

    /**
     * 鍒犻櫎鏁版嵁锛屼粠缂撳瓨涓垹闄?     * @param name
     */
    @RequestMapping(value = "/del", method = RequestMethod.PUT)
    @CacheEvict(value = "users", key = "#name", condition = "#name != null")
    public void deleteUserByName(@RequestParam(value = "name") String name) {

    }
}

鍚姩绋嬪簭锛岀敤postman杩涜娴嬭瘯

1锛夋墽琛宎dd鏂规硶锛屽皢淇℃伅瀛樺叆缂撳瓨

鎶€鏈浘鐗? src=

2锛夋墽琛宷uery鏂规硶锛屾煡鐪媍onsole

鎶€鏈浘鐗? src=

 

 鎶€鏈浘鐗? src=

3锛夋墽琛宒el鏂规硶锛屽垹闄ょ紦瀛?/p>

鎶€鏈浘鐗? src=

4)鎵цquery鏂规硶锛屾煡鐪媍onsole

鎶€鏈浘鐗? src=

 

 5銆丂CacheConfig

 鎵€鏈夌殑@Cacheable锛堬級閲岄潰閮芥湁涓€涓獀alue锛濃€渪xx鈥濈殑灞炴€э紝杩欐樉鐒跺鏋滄柟娉曞浜嗭紝鍐欒捣鏉ヤ篃鏄尯绱殑锛屽鏋滃彲浠ヤ竴娆℃€у0鏄庡畬 閭e氨鐪佷簨浜嗭紝 鎵€浠ワ紝鏈変簡@CacheConfig杩欎釜閰嶇疆锛屼綔鐢ㄥ湪绫讳笂闈紝鍙互灏嗕笂闈㈢殑Controller绫昏繘琛屾敼閫?/p>

鍦ㄤ笂闈㈢殑绋嬩腑鏂板缓RedisCachableController1

package com.redis.sentinel.demo.controller;

import com.redis.sentinel.demo.model.User;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @date 2019/03/21
 */
@RestController
@RequestMapping("/redis-sentinel-cache2")
@CacheConfig(cacheNames = "users")
public class RedisCachableController1 {

    private Map<String, Object> userList = new HashMap<>();
    /**
     * 鎸塳ey涓簎ser.name杩涜缂撳瓨
     * @param user
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @CachePut(key = "#user.name")
    public User addUser(@RequestBody User user) {
        userList.put(user.getName(), user);
        return user;
    }

    /**
     * 缁勫悎澶氫釜Cache娉ㄨВ浣跨敤,鎸塽ser.name鍜寀ser.sex涓や釜缁村害杩涜缂撳瓨
     * @param user
     * @return
     */
    @RequestMapping(value = "/add2", method = RequestMethod.POST)
    @Caching(put = {
            @CachePut( key = "#user.name"),
            @CachePut( key = "#user.sex")
    })
    public User addUser2(@RequestBody User user) {
        userList.put(user.getName(), user);
        return user;
    }


    /**
     * 鍏堜粠缂撳瓨鏌ユ暟鎹紝濡傛灉缂撳瓨娌℃湁锛屽垯鍒皍serList閲岄潰鍘绘嬁銆?     * @param name
     * @return
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    @Cacheable( key = "#p0", condition = "#p0 != null")
    public User queryUserByName(@RequestParam(value = "name") String name) {
        System.out.println("濡傛灉缂撳瓨娌℃湁锛屼粠map閲岄潰鑾峰彇");
        User user = (User) userList.get(name);
        return user;
    }

    /**
     * 鍒犻櫎鏁版嵁锛屼粠缂撳瓨涓垹闄?     * @param name
     */
    @RequestMapping(value = "/del", method = RequestMethod.PUT)
    @CacheEvict(key = "#name", condition = "#name != null")
    public void deleteUserByName(@RequestParam(value = "name") String name) {

    }
}

 

SpEL琛ㄨ揪寮?/h1>

Spring Cache鎻愪緵浜嗕竴浜涗緵鎴戜滑浣跨敤鐨凷pEL涓婁笅鏂囨暟鎹紝涓嬭〃鐩存帴鎽樿嚜Spring瀹樻柟鏂囨。锛?/p>

鍚嶇О浣嶇疆鎻忚堪绀轰緥
methodName root瀵硅薄 褰撳墠琚皟鐢ㄧ殑鏂规硶鍚?/td> root.methodName
method root瀵硅薄 褰撳墠琚皟鐢ㄧ殑鏂规硶 root.method.name
target root瀵硅薄 褰撳墠琚皟鐢ㄧ殑鐩爣瀵硅薄 root.target
targetClass root瀵硅薄 褰撳墠琚皟鐢ㄧ殑鐩爣瀵硅薄绫?/td> root.targetClass
args root瀵硅薄 褰撳墠琚皟鐢ㄧ殑鏂规硶鐨勫弬鏁板垪琛?/td> root.args[0]
caches root瀵硅薄 褰撳墠鏂规硶璋冪敤浣跨敤鐨勭紦瀛樺垪琛紙濡侤Cacheable(value={鈥渃ache1鈥? 鈥渃ache2鈥潁)锛夛紝鍒欐湁涓や釜cache root.caches[0].name
argument name 鎵ц涓婁笅鏂?/td> 褰撳墠琚皟鐢ㄧ殑鏂规硶鐨勫弬鏁帮紝濡俧indById(Long id)锛屾垜浠彲浠ラ€氳繃#id鎷垮埌鍙傛暟 user.id
result 鎵ц涓婁笅鏂?/td> 鏂规硶鎵ц鍚庣殑杩斿洖鍊硷紙浠呭綋鏂规硶鎵ц涔嬪悗鐨勫垽鏂湁鏁堬紝濡傗€榰nless鈥欙紝鈥檆ache evict鈥欑殑beforeInvocation=false锛?/td> result

以上是关于銆愯浆銆?springboot鏁村悎redis-sentinel鏀寔Cache娉ㄨВ的主要内容,如果未能解决你的问题,请参考以下文章

銆愯浆銆慾ava.io.Closeable鎺ュ彛

銆愯浆銆戙€怱alesfoece銆慉pproval Process 鍦?Apex 涓殑浣跨敤

銆愯浆銆戝悓婧愮瓥鐣ュ拰璺ㄥ煙璇锋眰瑙e喅鏂规

銆愯浆銆慔Base涓璟ookeeper锛孯egionServer锛孧aster锛孋lient涔嬮棿鍏崇郴

springboot鏁村悎activiti

Springboot鏁村悎Junit