主要是分为三部分,application 配置文件、redis的config文件以及redis的操作文件

一、maven 引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>1.5.7.RELEASE</version>
</dependency>

二、application 配置

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456
spring.redis.timeout=5000

三、redisconfig 配置

package com.xiang.redis;import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Bean@SuppressWarnings("all")public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();template.setConnectionFactory(factory);// 开启事务template.setEnableTransactionSupport(true);Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);jackson2JsonRedisSerializer.setObjectMapper(om);StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();// key采用String的序列化方式template.setKeySerializer(stringRedisSerializer);// hash的key也采用String的序列化方式template.setHashKeySerializer(stringRedisSerializer);// value序列化方式采用jacksontemplate.setValueSerializer(jackson2JsonRedisSerializer);// hash的value序列化方式采用jacksontemplate.setHashValueSerializer(jackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}}

四、redis 操作类

package com.xiang.redis;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;@Component("redisOperator")
@Configuration
public class RedisOperator {@Autowiredprivate StringRedisTemplate redisTemplate;/*** 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。** @param key* @return*/public long ttl(String key) {return redisTemplate.getExpire(key);}/*** 实现命令:expire 设置过期时间,单位秒** @param key* @return*/public void expire(String key, long timeout) {redisTemplate.expire(key, timeout, TimeUnit.SECONDS);}/*** 实现命令:INCR key,增加key一次** @param key* @return*/public long incr(String key, long delta) {return redisTemplate.opsForValue().increment(key, delta);}/*** 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key*/public Set<String> keys(String pattern) {return redisTemplate.keys(pattern);}/*** 实现命令:DEL key,删除一个key** @param key*/public void del(String key) {redisTemplate.delete(key);}/*** =============  Hash(哈希表)=================*//*** 命令:HSET key field value,** @param key   key值* @param field 此key下的域field* @param value 域field的值*/public void hset(String key, String field, Object value) {redisTemplate.opsForHash().put(key, field, value);}/*** 实现命令:HGET key field,返回哈希表 key中给定域 field的值** @param key* @param field 获取key下域* @return*/public String hget(String key, String field) {return (String) redisTemplate.opsForHash().get(key, field);}/*** 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。** @param key* @param fields*/public void hdel(String key, Object... fields) {redisTemplate.opsForHash().delete(key, fields);}/*** 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。** @param key* @return*/public Map<Object, Object> hgetall(String key) {return redisTemplate.opsForHash().entries(key);}// ===============================list=================================/*** 获取list缓存的内容** @param key   键* @param start 开始* @param end   结束 0 到 -1代表所有值* @return*/public List<String> 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 表头,1 第二个元素,依次类推;index<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;}}/*** 将list放入缓存** @param key   键* @param value 值* @return*/public boolean lSet(String key, String value) {try {redisTemplate.opsForList().rightPush(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** @param key   键* @param value 值* @param time  时间(秒)* @return*/public boolean lSet(String key, String value, long time) {try {redisTemplate.opsForList().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<String> value) {try {redisTemplate.opsForList().rightPushAll(key, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 将list放入缓存** @param key   键* @param value 值* @param time  时间(秒)* @return*/public boolean lSet(String key, List<String> 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, String value) {try {redisTemplate.opsForList().set(key, index, value);return true;} catch (Exception e) {e.printStackTrace();return false;}}/*** 移除N个值为value** @param key   键* @param count 移除多少个* @param value 值* @return 移除的个数*/public long lRemove(String key, long count, Object value) {try {return redisTemplate.opsForList().remove(key, count, value);} catch (Exception e) {e.printStackTrace();return 0;}}}

springboot 中使用redis 01相关推荐

  1. springboot中使用redis详解

    一.redis简介 redis是一款高性能key-value(键值对)内存型数据库,是非关系型数据库的一种,它采用单线程的架构方式,避免了多线程存在的锁处理造成的资源耗费,读取速度非常快,非常适合变化 ...

  2. redis:01入门指南以及在springboot中使用redis

    https://redis.io/download step1:参考官网的安装很简单 wget http://download.redis.io/releases/redis-5.0.6.tar.gz ...

  3. 你知道如何在springboot中使用redis吗

    特别说明:本文针对的是新版 spring boot 2.1.3,其 spring data 依赖为 spring-boot-starter-data-redis,且其默认连接池为 lettuce ​  ...

  4. SpringBoot中集成Redis实现对redis中数据的解析和存储

    场景 SpringBoot中操作spring redis的工具类: SpringBoot中操作spring redis的工具类_霸道流氓气质的博客-CSDN博客 上面讲的操作redis的工具类,但是对 ...

  5. SpringBoot中使用Redis保存对象或集合

    1,引入SpringBoot中Redis依赖 <!-- redis --> <dependency><groupId>org.springframework.boo ...

  6. SpringBoot中使用redis事务

    本文基于SpringBoot 2.X 事务在关系型数据库的开发中经常用到,其实非关系型数据库,比如redis也有对事务的支持,本文主要探讨在SpringBoot中如何使用redis事务. 事务的相关介 ...

  7. Docker中搭建redis分片集群,搭建redis哨兵结构,实现springboot中对redis分片集群、哨兵结构的访问,Redis缓存雪崩、缓存击穿处理(非关系型数据库技术课程 第十二周)

    文章目录 一.要求: 二.知识总结 缓存雪崩 解决方案 docker中redis分片集群搭建 配置好配置文件 redis-6380.conf redis-6381.conf redis-6382.co ...

  8. SpringBoot中使用Redis数据库

    1.可以下载Iedis插件浏览Redis中数据 2.下好安装好Redis,开启Redis服务 3.配置Redis Server信息 查看Redis数据库里面数据 4.在项目中使用Redis数据库 1) ...

  9. Springboot 中的Redis 事务使用

    简述 有一些场景我们可以在一段代码中多次操作redis,每次请求Redis都要去Jedis/Lettuce连接池申请一个连接请求一次redis服务进行缓存操作. 这样不仅有网络的消耗,假如在redis ...

最新文章

  1. 无线网***工具进攻方法及防范技巧
  2. php查询当前session,php查看当前Session的ID方法
  3. mysql单库tps_MySQL数据库三个关键性能指标--TPS\QPS\IOPS
  4. navicat连接本地MySQL8.0.19报1251错误的解决办法
  5. 如何使用SAP CRM中间件下载customer material数据
  6. iOS开发之普通网络异步请求与文件下载方法
  7. python包管理机制_Go 1.5之前的多种包管理机制简介(
  8. 【OpenCV】OpenCV访问像素点的三种方式
  9. POJ1060 HDU1343 ZOJ1026 UVALive2323 Modular multiplication of polynomials题解
  10. AcWing 902. 最短编辑距离(线性DP)
  11. 教师职称考计算机模块,2015教师职称计算机考试模块.doc
  12. STM3库文件 hal_uart.c的使用
  13. Ordering disordered structures
  14. Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !
  15. 工业互联网:7  项目生命周期管理(1)
  16. 国内外PaaS案例解析、赛道、趋势
  17. 最佳实践 | 如何提高落地页的转化率?这里有4个策略
  18. 玩转OpenStack网络Neutron(1)--热身
  19. 深度学习各类性能指标含义解释
  20. python 爬虫 --字符编写问题

热门文章

  1. 计算机科学与技术秃头,传说中的五大专业之秃头篇!
  2. c++ hough变换代码_FPGA时序结构的LBT变换控制器设计
  3. Eclipse Theia技术揭秘——脚手架源码分析
  4. [附源码]JAVA+ssm计算机毕业设计财务管理系统(程序+Lw)
  5. Springcloud什么鬼错误
  6. 西电计算机导论名师,西电周佳社、王泉两名教授被评为省级教学名师
  7. 照片怎么设置成1寸大小?修改图片像素大小的方法
  8. flash actionscript3.0 游戏人物走动
  9. 使用Mybatis实现点菜功能
  10. 新概念二册 Lesson 40 food and talk进餐与交谈( 现在进行时vs将来进行时+虚拟语气假设现在)