首頁>技術>

引入依賴

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-redis</artifactId></dependency>1234
RedisTemplate五種資料結構的操作

redisTemplate.opsForValue(); //操作字串redisTemplate.opsForHash(); //操作hashredisTemplate.opsForList(); //操作listredisTemplate.opsForSet(); //操作setredisTemplate.opsForZSet(); //操作有序zset

RedisTemplate方法講解

判斷key是否存在

	/**     * 判斷key是否存在     */    @GetMapping("haskey")    public boolean hasKey(String key) {        return redisTemplate.hasKey(key);    }

獲取指定的key的失效時間

	/**     * 指定key的失效時間     */    @GetMapping("expire")    public void expire(String key, long time) {        //引數一:key        //引數二:睡眠時間        //引數三:睡眠時間單位 TimeUnit.DAYS 天 TimeUnit.HOURS 小時 。。。        redisTemplate.expire(key, time, TimeUnit.MINUTES);    }

根據key獲取過期時間

	/**     * 根據key獲取過期時間     */    @GetMapping("getexpire")    public long getExpire(String key) {        Long expire = redisTemplate.getExpire(key);        return expire;    }

根據key刪除reids中快取資料

	/**     * 根據key刪除reids中快取資料     */    @GetMapping("delredisbykey")    public void delete(String key) {        redisTemplate.delete(key);    }

儲存和讀取String

	/**     * 儲存和讀取String     */    @GetMapping("stringredisdemo")    public String stringredisdemo() {        //設定過期時間為1分鐘        redisTemplate.opsForValue().set("key1", "value1", 1, TimeUnit.MINUTES);        redisTemplate.opsForValue().set("key2", "value2");        redisTemplate.opsForValue().set("key3", "value3");        //讀取redis資料        String result1 = redisTemplate.opsForValue().get("key1").toString();        String result2 = redisTemplate.opsForValue().get("key2").toString();        String result3 = redisTemplate.opsForValue().get("key3").toString();        System.out.println("快取結果為:result:" + result1 + "  " + result2 + "   " + result3);        return "快取結果為:result:" + result1 + "  " + result2 + "   " + result3;    }

儲存和讀取list

	/**     * 儲存和讀取list     */    @GetMapping("listredisdemo")    public String listredisdemo() {        List<String> list1 = new ArrayList<>();        list1.add("a1");        list1.add("a2");        list1.add("a3");        List<String> list2 = new ArrayList<>();        list2.add("b1");        list2.add("b2");        list2.add("b3");        redisTemplate.opsForList().leftPush("listkey1", list1);        redisTemplate.opsForList().rightPush("listkey2", list2);        List<String> resultList1 = (List<String>) redisTemplate.opsForList().leftPop("listkey1");        List<String> resultList2 = (List<String>) redisTemplate.opsForList().rightPop("listkey2");        System.out.println("resultList1:" + resultList1);        System.out.println("resultList2:" + resultList2);        return "成功";    }123456789101112131415161718192021

Hash結構,儲存和讀取map

	/**     * Hash結構,儲存和讀取map     */    @GetMapping("mapredisdemo")    public String mapredisdemo() {        Map<String, String> map = new HashMap<>();        map.put("key1", "value1");        map.put("key2", "value2");        map.put("key3", "value3");        map.put("key4", "value4");        map.put("key5", "value5");        redisTemplate.opsForHash().putAll("map1", map);        Map<String, String> resultMap = redisTemplate.opsForHash().entries("map1");        List<String> reslutMapList = redisTemplate.opsForHash().values("map1");        Set<String> resultMapSet = redisTemplate.opsForHash().keys("map1");        String value = (String) redisTemplate.opsForHash().get("map1", "key1");        System.out.println("value:" + value);        System.out.println("resultMapSet:" + resultMapSet);        System.out.println("resultMap:" + resultMap);        System.out.println("resulreslutMapListtMap:" + reslutMapList);        return "成功";    }12345678910111213141516171819202122

儲存和讀取Set

	/**     * 儲存和讀取Set     */    @GetMapping("setredisdemo")    public String getredisdemo() {        SetOperations<String, String> set = redisTemplate.opsForSet();        set.add("key1", "value1");        set.add("key1", "value2");        set.add("key1", "value3");        Set<String> resultSet = redisTemplate.opsForSet().members("key1");        System.out.println("resultSet:" + resultSet);        return "resultSet:" + resultSet;    }12345678910111213

儲存和讀取zset

	/**     * 儲存和讀取zset     */    @GetMapping("zsetredisdemo")    public String zsetredisdemo() {        ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>("zset-5", 9.6);        ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("zset-6", 9.9);        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>();        tuples.add(objectTypedTuple1);        tuples.add(objectTypedTuple2);        System.out.println(redisTemplate.opsForZSet().add("zset1", tuples));        System.out.println(redisTemplate.opsForZSet().range("zset1", 0, -1));        return "成功";    }1234567891011121314
完整示例程式碼
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.DefaultTypedTuple;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.core.SetOperations;import org.springframework.data.redis.core.ZSetOperations;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.*;import java.util.concurrent.TimeUnit;@RestControllerpublic class ReidsDemo {    @Autowired    RedisTemplate redisTemplate;    /**     * 指定key的失效時間     */    @GetMapping("expire")    public void expire(String key, long time) {        //引數一:key        //引數二:睡眠時間        //引數三:睡眠時間單位 TimeUnit.DAYS 天 TimeUnit.HOURS 小時 。。。        redisTemplate.expire(key, time, TimeUnit.MINUTES);    }    /**     * 根據key獲取過期時間     */    @GetMapping("getexpire")    public long getExpire(String key) {        Long expire = redisTemplate.getExpire(key);        return expire;    }    /**     * 判斷key是否存在     */    @GetMapping("haskey")    public boolean hasKey(String key) {        return redisTemplate.hasKey(key);    }    /**     * 根據key刪除reids中快取資料     */    @GetMapping("delredisbykey")    public void delete(String key) {        redisTemplate.delete(key);    }    /**     * 儲存和讀取String     */    @GetMapping("stringredisdemo")    public String stringredisdemo() {        //設定過期時間為1分鐘        redisTemplate.opsForValue().set("key1", "value1", 1, TimeUnit.MINUTES);        redisTemplate.opsForValue().set("key2", "value2");        redisTemplate.opsForValue().set("key3", "value3");        //讀取redis資料        String result1 = redisTemplate.opsForValue().get("key1").toString();        String result2 = redisTemplate.opsForValue().get("key2").toString();        String result3 = redisTemplate.opsForValue().get("key3").toString();        System.out.println("快取結果為:result:" + result1 + "  " + result2 + "   " + result3);        return "快取結果為:result:" + result1 + "  " + result2 + "   " + result3;    }    /**     * 儲存和讀取list     */    @GetMapping("listredisdemo")    public String listredisdemo() {        List<String> list1 = new ArrayList<>();        list1.add("a1");        list1.add("a2");        list1.add("a3");        List<String> list2 = new ArrayList<>();        list2.add("b1");        list2.add("b2");        list2.add("b3");        redisTemplate.opsForList().leftPush("listkey1", list1);        redisTemplate.opsForList().rightPush("listkey2", list2);        List<String> resultList1 = (List<String>) redisTemplate.opsForList().leftPop("listkey1");        List<String> resultList2 = (List<String>) redisTemplate.opsForList().rightPop("listkey2");        System.out.println("resultList1:" + resultList1);        System.out.println("resultList2:" + resultList2);        return "成功";    }    /**     * Hash結構,儲存和讀取map     */    @GetMapping("mapredisdemo")    public String mapredisdemo() {        Map<String, String> map = new HashMap<>();        map.put("key1", "value1");        map.put("key2", "value2");        map.put("key3", "value3");        redisTemplate.opsForHash().putAll("map1", map);        Map<String, String> resultMap = redisTemplate.opsForHash().entries("map1");        List<String> reslutMapList = redisTemplate.opsForHash().values("map1");        Set<String> resultMapSet = redisTemplate.opsForHash().keys("map1");        String value = (String) redisTemplate.opsForHash().get("map1", "key1");        System.out.println("value:" + value);        System.out.println("resultMapSet:" + resultMapSet);        System.out.println("resultMap:" + resultMap);        System.out.println("resulreslutMapListtMap:" + reslutMapList);        return "成功";    }    /**     * 儲存和讀取Set     */    @GetMapping("setredisdemo")    public String getredisdemo() {        SetOperations<String, String> set = redisTemplate.opsForSet();        set.add("key1", "value1");        set.add("key1", "value2");        set.add("key1", "value3");        Set<String> resultSet = redisTemplate.opsForSet().members("key1");        System.out.println("resultSet:" + resultSet);        return "resultSet:" + resultSet;    }    /**     * 儲存和讀取zset     */    @GetMapping("zsetredisdemo")    public String zsetredisdemo() {        ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<>("zset-5", 9.6);        ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<>("zset-6", 9.9);        Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<>();        tuples.add(objectTypedTuple1);        tuples.add(objectTypedTuple2);        System.out.println(redisTemplate.opsForZSet().add("zset1", tuples));        System.out.println(redisTemplate.opsForZSet().range("zset1", 0, -1));        return "成功";    }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
序列化

直接貼上在專案中即可

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.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.RedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;/**序列化 */@Configurationpublic class MyRedisConfig {    @Bean(name = "redisTemplate")    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();        //參照StringRedisTemplate內部實現指定序列化器        redisTemplate.setConnectionFactory(redisConnectionFactory);        redisTemplate.setKeySerializer(keySerializer());        redisTemplate.setHashKeySerializer(keySerializer());        redisTemplate.setValueSerializer(valueSerializer());        redisTemplate.setHashValueSerializer(valueSerializer());        return redisTemplate;    }    private RedisSerializer<String> keySerializer(){        return new StringRedisSerializer();    }    //使用Jackson序列化器    private RedisSerializer<Object> valueSerializer(){        return new GenericJackson2JsonRedisSerializer();    }    }

讀者福利:關注小編+私信回覆【專案】獲取整理好的100+個Java專案影片+原始碼+筆記

原文:blog.csdn.net/xiaozhezhe0470/article/details/111870478

11
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 安全 第11講——IDEA加密演算法的介紹和使用