spring如何整合redis使用-创新互联

小编给大家分享一下spring如何整合redis使用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联建站专注于企业营销型网站建设、网站重做改版、金坛网站定制设计、自适应品牌网站建设、HTML5建站电子商务商城网站建设、集团公司官网建设、成都外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为金坛等各大城市提供网站开发制作服务。

1.简单介绍

redis 是基于C语言开发。

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

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

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

<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

<?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

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:有序集合)

具体使用如下,上代码:

//添加字符串 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));

以上是“spring如何整合redis使用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网页标题:spring如何整合redis使用-创新互联
文章地址:https://www.cdcxhl.com/article4/ccchoe.html

成都网站建设公司_创新互联,为您提供软件开发网站设计公司商城网站外贸建站搜索引擎优化网站导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

绵阳服务器托管