spring 整合redis 简单使用

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:



1.简单介绍

redis 是基于C语言开发。

redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。

redis 是一个 缓存数据库(片面的理解) 既可以做缓存,也可以将数据持久化到磁盘中!

 2.pom.xml 引入相关jar (曾经因jar 版本问题出现报错,请注意)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
< dependency >
     < groupId >org.apache.commons</ groupId >
     < artifactId >commons-pool2</ artifactId >
     < version >2.2</ version >
</ dependency >
 
 
< dependency >
     < groupId >org.springframework.data</ groupId >
     < artifactId >spring-data-redis</ artifactId >
     < version >1.7.5.RELEASE</ version >
</ dependency >
 
< dependency >
     < groupId >redis.clients</ groupId >
     < artifactId >jedis</ artifactId >
     < version >2.9.0</ version >
</ dependency >


3.spring-redis.xml 配置文件,配置关键bean  redisTemplate

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
<? xml  version = "1.0"  encoding = "UTF-8" ?>  
< beans  xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"  xmlns:p = "http://www.springframework.org/schema/p"
   xmlns:mvc = "http://www.springframework.org/schema/mvc"  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xmlns:tx = "http://www.springframework.org/schema/tx"  xmlns:util = "http://www.springframework.org/schema/util"
   xmlns:aop = "http://www.springframework.org/schema/aop"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-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/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd">
     
<!--    <context:property-placeholder location="classpath:redis-config.properties"/>    
       -->
     
     
     < bean  id = "jedisPoolConfig"  class = "redis.clients.jedis.JedisPoolConfig" >
          < property  name = "maxIdle"  value = "${redis.maxIdle}"  /> 
         < property  name = "maxTotal"  value = "${redis.maxTotal}"  /> 
         < property  name = "blockWhenExhausted"  value = "true"  /> 
         < property  name = "maxWaitMillis"  value = "${redis.maxWaitMillis}"  /> 
         < property  name = "testOnBorrow"  value = "${redis.testOnBorrow}"  /> 
     </ bean >
   
   < bean  id = "jedisConnectionFactory"  class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
     < property  name = "hostName"  value = "${redis.hostname}"  /> 
     < property  name = "port"  value = "${redis.port}" /> 
     < property  name = "poolConfig"  ref = "jedisPoolConfig"  /> 
     < property  name = "usePool"  value = "true" /> 
   </ bean
   
   < bean  id = "redisTemplate"  class = "org.springframework.data.redis.core.RedisTemplate" >  
     < property  name = "connectionFactory"   ref = "jedisConnectionFactory"  />  
     < property  name = "keySerializer" >  
       < bean  class = "org.springframework.data.redis.serializer.StringRedisSerializer"  />  
     </ property >   
     < property  name = "valueSerializer" >  
       < bean  class = "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"  />  
     </ property >  
     < property  name = "hashKeySerializer" >   
       < bean  class = "org.springframework.data.redis.serializer.StringRedisSerializer" />   
     </ property >  
     < property  name = "hashValueSerializer" >  
       < bean  class = "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />   
     </ property
    </ bean
   
   
</ beans >



上文中使用到的配置文件 redis-config.properteis

1
2
3
4
5
6
redis.maxIdle=1
redis.maxTotal=5
redis.maxWaitMillis=30000
redis.testOnBorrow=true
redis.hostname=127.0.0.1
redis.port=6379


4.redis 有4个关键的接口如下


private ValueOperations<K, V> valueOps;

private ListOperations<K, V> listOps;

private SetOperations<K, V> setOps;

private ZSetOperations<K, V> zSetOps;



分别对应redis的数据类型:string(字符串),hash(哈希),list(列表),set(集合)及zset(sorted set:有序集合)


具体使用如下,上代码:

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
//添加字符串
ValueOperations<String, String> value =  this .redisTemplate.opsForValue();
value.set( "hello" "讨厌" );
System.out.println(value.get( "hello" ));
 
 
//添加 一个 hash集合
HashOperations<String, Object, Object>  hash =redisTemplate.opsForHash();
hash.put( "沃尔玛" , "水果" "苹果" );
hash.put( "沃尔玛" , "饮料" "红牛" );
System.out.println(hash.entries( "沃尔玛" ));
 
 
//添加一个list 集合
ListOperations<String, Object> list = redisTemplate.opsForList();
list.rightPush( "课程" "chinese" );
list.rightPush( "课程" "englise" );
System.out.println(list.range( "lpList" 0 1 ));
 
 
//添加 一个 set 集合
SetOperations<String, Object> set = redisTemplate.opsForSet();
set.add( "lpSet" "lp" );
set.add( "lpSet" "26" );
set.add( "lpSet" "178cm" );
//输出 set 集合
System.out.println(set.members( "lpSet" ));
 
 
//添加有序的 set 集合
ZSetOperations<String, Object> zset = redisTemplate.opsForZSet();
zset.add( "lpZset" "lp" 0 );
zset.add( "lpZset" "26" 2 );
zset.add( "lpZset" "178cm" 1 );
//输出有序 set 集合
System.out.println(zset.rangeByScore( "lpZset" 0 2 ));


      本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1976501,如需转载请自行联系原作者


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1月前
|
NoSQL 安全 Java
Spring Boot3整合Redis
Spring Boot3整合Redis
60 1
|
1月前
|
存储 缓存 Java
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
|
1月前
|
缓存 NoSQL Java
spring cache整合redis实现springboot项目中的缓存功能
spring cache整合redis实现springboot项目中的缓存功能
45 1
|
1月前
|
存储 NoSQL Java
[Redis]——Spring整合Redis(SpringDataRedis)
[Redis]——Spring整合Redis(SpringDataRedis)
|
1月前
|
监控 NoSQL Java
Spring Boot集成Redis启动失败【Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.G】
Spring Boot集成Redis启动失败【Caused by: java.lang.ClassNotFoundException: org.apache.commons.pool2.impl.G】
|
1月前
|
NoSQL Java 定位技术
|
1月前
|
存储 NoSQL Java
如何使用Spring Boot与Redis集成
2月更文挑战第12天】
53 0
|
NoSQL Java 数据库
|
Java Spring 数据格式
spring 整合redis
用的是最新的jedis-2.6.2.jar这个包,这个和以前的有点不同。还需要添加spring-data-redis-1.2.1.RELEASE.jar和commons-pool2-2.3.jar。 在类路径下创建spring-redis-config.
923 0
|
NoSQL Java Redis
redis和spring整合
pom构建: [html] view plain copy  print? &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;   &lt;groupId&gt;com.x.redis&lt;/groupId&gt;   &lt;artifactId&gt;springredis&l
1023 0

热门文章

最新文章