RedisTemplate的使用
Redis 数据结构简介
Redis可以存储键与5种不同数据结构类型之间的映射,这5种数据结构类型分别为String(字符串)、List(列表)、Set(集合)、Hash(散列)和 Zset(有序集合)。
下面来对这5种数据结构类型作简单的介绍:
结构类型 | 结构存储的值 | 结构的读写能力 |
---|---|---|
String | 可以是字符串、整数或者浮点数 | 对整个字符串或者字符串的其中一部分执行操作;对象和浮点数执行自增(increment)或者自减(decrement) |
List | 一个链表,链表上的每个节点都包含了一个字符串 | 从链表的两端推入或者弹出元素;根据偏移量对链表进行修剪(trim);读取单个或者多个元素;根据值来查找或者移除元素 |
Set | 包含字符串的无序收集器(unorderedcollection),并且被包含的每个字符串都是独一无二的、各不相同 | 添加、获取、移除单个元素;检查一个元素是否存在于某个集合中;计算交集、并集、差集;从集合里卖弄随机获取元素 |
Hash | 包含键值对的无序散列表 | 添加、获取、移除单个键值对;获取所有键值对 |
Zset | 字符串成员(member)与浮点数分值(score)之间的有序映射,元素的排列顺序由分值的大小决定 | 添加、获取、删除单个元素;根据分值范围(range)或者成员来获取元素 |
Redis 5种数据结构的概念大致介绍到这边,下面将结合Spring封装的RedisTemplate来对这5种数据结构的运用进行演示
1.在maven项目中添加引用依赖
<!-- redis客户端--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> </dependency>
2. 配置文件
准备redis相关的配置参数,常见的有host, port, password, timeout…,下面是一份简单的配置,并给出了相应的含义
1 2 3 4 5 6 7 8 9 10 | redis.host=10.10.130.10 #10.10.130.10 10.10.131.27 #redis.password=pass redis.password=pass redis.timeout=3000 redis.port=6379 redis.maxIdle=200 redis.minIdle=0 redis.maxTotal=200 redis.max-wait-millis=1000 |
3. spring-redis.xml的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:context= "http://www.springframework.org/schema/context" xsi:schemaLocation=" http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-3.0.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id= "poolConfig" class = "redis.clients.jedis.JedisPoolConfig" > <property name= "maxIdle" value= "${redis.maxIdle}" /> <property name= "minIdle" value= "${redis.minIdle}" /> <property name= "maxTotal" value= "${redis.maxTotal}" /> <property name= "maxWaitMillis" value= "${redis.max-wait-millis}" /> </bean> <bean id= "redisConnectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name= "${redis.host}" p:port= "${redis.port}" p:password= "${redis.password}" p:database= "0" p:timeout= "${redis.timeout}" p:pool-config-ref= "poolConfig" /> <bean id= "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate" > <property name= "connectionFactory" ref= "redisConnectionFactory" /> <property name= "defaultSerializer" ref= "jdkSerializationRedisSerializer" /> </bean> <bean id= "stringRedisTemplate" class = "org.springframework.data.redis.core.StringRedisTemplate" > <property name= "connectionFactory" ref= "redisConnectionFactory" /> </bean> <bean id= "jdkSerializationRedisSerializer" class = " org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" ></bean> </beans> |
4. spring-redis.xml的导入
在service-context.xml导入spring-redis.xml加入项目中使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | <?xml version= "1.0" encoding= "UTF-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:tx= "http://www.springframework.org/schema/tx" xmlns:aop= "http://www.springframework.org/schema/aop" xmlns:context= "http://www.springframework.org/schema/context" xmlns:security= "http://www.springframework.org/schema/security" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-4.0.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx-4.0.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-4.0.xsd http: //www.springframework.org/schema/security http: //www.springframework.org/schema/security/spring-security-3.0.3.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-3.0.xsd" default -autowire= "byName" default -lazy-init= "true" > <description>Spring-database配置</description> <!-- 自动扫描 --> <context:component-scan base- package = "com.gta.scm" > <context:exclude-filter type= "annotation" expression= "org.springframework.stereotype.Controller" /> </context:component-scan> <bean id= "propertyConfigurer" class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > <property name= "locations" > <list> <value>classpath*:*.properties</value> </list> </property> </bean> <bean id= "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > </bean> <!-- redis component , depends-on:config --> <!-- <bean id= "sharedJedisPool" class = "com.gta.scm.component.common.redis.ShardedRedisPool" /> --> <!-- mybatis配置 --> <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" > <property name= "dataSource" ref= "dataSource" /> <property name= "configLocation" value= "classpath:mybatis-config.xml" /> </bean> <!-- 定义事务 --> <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "dataSource" ref= "dataSource" /> </bean> <!-- 配置 Annotation 驱动,扫描 @Transactional 注解的类定义事务 --> <tx:annotation-driven transaction-manager= "transactionManager" proxy-target- class = "true" /> <bean id= "myBatisDao" class = "com.gta.scm.component.ibatis.BaseDao" /> <!-- <bean id= "dataSource" class = "com.gta.scm.component.datasource.factory.SuperDataSource" /> --> <!-- 数据源 --> <bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" > <property name= "driverClassName" value= "${driver}" /> <property name= "url" value= "${url}" /> <property name= "username" value= "${username}" /> <property name= "password" value= "${password}" /> <!-- 初始化连接大小 --> <property name= "initialSize" value= "${initialSize}" ></property> <!-- 连接池最大数量 --> <property name= "maxActive" value= "${maxActive}" ></property> <!-- 连接池最大空闲 --> <property name= "maxIdle" value= "${maxIdle}" ></property> <!-- 连接池最小空闲 --> <property name= "minIdle" value= "${minIdle}" ></property> <!-- 获取连接最大等待时间 --> <property name= "maxWait" value= "${maxWait}" ></property> </bean> < import resource= "ehcache-context.xml" /> <strong> < import resource= "redis-context.xml" /></strong> < import resource= "websocket-context.xml" /> < import resource= "print-context.xml" /> </beans> |
4.RedisTemplate的具体使用
因为项目使用的地方不多,所以没有写公共类,直接调用了
导入引用import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
a.查询是否有记录,一定要进行为空判断,要不然有时候会报错的
1 2 3 4 5 | ValueOperations<String, EBankInfoRsp> valueops = redisTemplate.opsForValue(); if (valueops.size(account)> 0 ) { eBankInfo = valueops.get(account); } |
b.进行添加
1 2 3 | ValueOperations<String, EBankInfoRsp> valuSet = redisTemplate.opsForValue(); valuSet.set(account, ebank); redisTemplate.expire(account, 1 , TimeUnit.HOURS); |
c.删除
1 2 3 | if (eBankInfo != null ){ redisTemplate.delete(companyInitData.getBankAccount()); } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理