一、引入依赖

<!-- 缓存 -->

<dependency>

        <groupId>redis.clients</groupId>

        <artifactId>jedis</artifactId>

        <version>2.8.1</version>

</dependency>

<dependency>

        <groupId>org.springframework.data</groupId>

        <artifactId>spring-data-redis</artifactId>

        <version>1.7.2.RELEASE</version>

</dependency>  

二、在src/main/resources下创建properties文件夹,建立redis-config.properties

redis.host=127.0.0.1

redis.port=6379

redis.pass=

redis.database=0

redis.maxIdle=300

redis.maxWait=3000

redis.testOnBorrow=true

三、在src/main/resources下创建spring文件夹 ,创建applicationContext-redis.xml

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"> <context:property-placeholder location="classpath*:properties/*.properties" /> <!-- redis 相关配置 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean> <bean id="JedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.pass}" p:pool-config-ref="poolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="JedisConnectionFactory" />
</bean> </beans>

maxWaitMillis:连接时的最大等待毫秒数

maxIdle :最大空闲数

testOnBorrow:在提取一个jedis实例时,是否提前进行验证操作;如果为true,则得到的jedis实例均是可用的;

四、值类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
publicclass TestValue {
@Autowired
private RedisTemplate redisTemplate;
@Test
publi cvoid setValue(){
redisTemplate.boundValueOps("name").set("itcast");
}
@Test
public void getValue(){
String str = (String) redisTemplate.boundValueOps("name").get();
System.out.println(str);
}
@Test
publi cvoid deleteValue(){
redisTemplate.delete("name");;
}
}

五、 Set类型操作

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:spring/applicationContext-redis.xml")
publicclass TestSet { @Autowired
private RedisTemplate redisTemplate; /**
* 存入值
*/
@Test
public void setValue(){
redisTemplate.boundSetOps("nameset").add("曹操");
redisTemplate.boundSetOps("nameset").add("刘备");
redisTemplate.boundSetOps("nameset").add("孙权");
} /**
* 提取值
*/
@Test
public void getValue(){
Setmembers = redisTemplate.boundSetOps("nameset").members();
System.out.println(members);
} /**
* 删除集合中的某一个值
*/
@Test
public void deleteValue(){
redisTemplate.boundSetOps("nameset").remove("孙权");
} /**
* 删除整个集合
*/
@Test
public void deleteAllValue(){
redisTemplate.delete("nameset");
}
}

六、 List类型操作

创建测试类TestList

(1)右压栈

/**
* 右压栈:后添加的对象排在后边
*/
@Test
public void testSetValue1(){
redisTemplate.boundListOps("namelist1").rightPush("刘备");
redisTemplate.boundListOps("namelist1").rightPush("关羽");
redisTemplate.boundListOps("namelist1").rightPush("张飞");
} /**
* 显示右压栈集合
*/
@Test
public void testGetValue1(){
Listlist = redisTemplate.boundListOps("namelist1").range(0, 10);
System.out.println(list);
}

运行结果:

[刘备, 关羽, 张飞]

(2)左压栈

/**
* 左压栈:后添加的对象排在前边
*/
@Test
public void testSetValue2(){
redisTemplate.boundListOps("namelist2").leftPush("刘备");
redisTemplate.boundListOps("namelist2").leftPush("关羽");
redisTemplate.boundListOps("namelist2").leftPush("张飞");
} /**
* 显示左压栈集合
*/
@Test
public void testGetValue2(){
Listlist = redisTemplate.boundListOps("namelist2").range(0, 10);
System.out.println(list);
}

运行结果:

[张飞, 关羽, 刘备]

(3)根据索引查询元素

    /**
* 查询集合某个元素
*/
@Test
public void testSearchByIndex(){
String s = (String) redisTemplate.boundListOps("namelist1").index(1);
System.out.println(s);
}

(4)移除某个元素的值

    /**
* 移除集合某个元素
*/
@Test
public void testRemoveByIndex(){
redisTemplate.boundListOps("namelist1").remove(1, "关羽");
}

七、Hash类型操作

创建测试类TestHash

(1)存入值

@Test
public void testSetValue(){
redisTemplate.boundHashOps("namehash").put("a", "唐僧");
redisTemplate.boundHashOps("namehash").put("b", "悟空");
redisTemplate.boundHashOps("namehash").put("c", "八戒");
redisTemplate.boundHashOps("namehash").put("d", "沙僧");
}

(2)提取所有的KEY 

@Test
public void testGetKeys(){
Sets = redisTemplate.boundHashOps("namehash").keys();
System.out.println(s);
}

运行结果:

[a, b, c, d]

(3)提取所有的值

@Test
public void testGetValues(){
Listvalues = redisTemplate.boundHashOps("namehash").values();
System.out.println(values);
}

运行结果:

[唐僧, 悟空, 八戒, 沙僧]

 

(4)根据KEY提取值

@Test
publi cvoid testGetValueByKey(){
Object object = redisTemplate.boundHashOps("namehash").get("b");
System.out.println(object);
}

运行结果:

悟空

 

(5)根据KEY移除值

@Test
public void testRemoveValueByKey(){
redisTemplate.boundHashOps("namehash").delete("c");
}

运行后再次查看集合内容:

[唐僧, 悟空, 沙僧]

spring-data-redis数据类型的更多相关文章

  1. spring mvc Spring Data Redis RedisTemplate [转]

    http://maven.springframework.org/release/org/springframework/data/spring-data-redis/(spring-data包下载) ...

  2. Spring Data Redis简介以及项目Demo,RedisTemplate和 Serializer详解

    一.概念简介: Redis: Redis是一款开源的Key-Value数据库,运行在内存中,由ANSI C编写,详细的信息在Redis官网上面有,因为我自己通过google等各种渠道去学习Redis, ...

  3. Spring Data Redis整体介绍 (一)

    为什么使用Spring Data Redis 首先Spring Data Redis 是Spring 框架提供的用于操作Redis的客户端. Spring框架是一个全栈Java程序框架,通过DI.AO ...

  4. spring boot通过Spring Data Redis集成redis

    在spring boot中,默认集成的redis是Spring Data Redis,Spring Data Redis针对redis提供了非常方便的操作模版RedisTemplate idea中新建 ...

  5. 使用Spring Data Redis时,遇到的几个问题

    需求: 1,保存一个key-value形式的结构到redis 2,把一个对象保存成hash形式的结构到redis 代码如下: // 保存key-value值         pushFrequency ...

  6. Redis与Spring Data Redis

    1.Redis概述 1.1介绍 官网:https://redis.io/ Redis是一个开源的使用ANSIC语言编写.支持网络.可基于内存 亦可持久化的日志型.Key-Value型的高性能数据库. ...

  7. spring data redis RedisTemplate操作redis相关用法

    http://blog.mkfree.com/posts/515835d1975a30cc561dc35d spring-data-redis API:http://docs.spring.io/sp ...

  8. Spring Data Redis—Pub/Sub(附Web项目源码)

    一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...

  9. Spring data redis的一个bug

    起因 前两天上线了一个新功能,导致线上业务的缓存总是无法更新,报错也是非常奇怪,redis.clients.jedis.exceptions.JedisConnectionException: Unk ...

  10. Spring Data Redis—Pub/Sub(附Web项目源码) (转)

    一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...

随机推荐

  1. linux系统编程之信号(一)

    今天起,开始新的知识的学习,对于上个系列进程的学习还差一个理论上的总结,这个会下次补回来,以便通过实践之后,再用理论将其巩固一下,好了,话不多说,开始进入这个主题的学习----信号,很重要,但不是太容 ...

  2. SQLyog(MYSQL图形化开发工具)

    安装:下载一个免安装的SQLyog,可以直接使用 使用:输入用户名.密码,点击连接按钮,进行访问MySQL数据库进行操作 在Query窗口中,输入SQL代码,选中要执行的SQL代码,按F8键运行,或按 ...

  3. jupytext library using in jupyter notebook

    目录 1. jupytext features 2. Way of using 3. usage 4. installation 1. jupytext features Jupytext can s ...

  4. python - alipay sdk 使用 及 注意点

    一. 在 点击跳转 这里拿到自己的 appid  和  支付宝公钥 ,    如果想要得到 支付宝的公钥 就需要获取 应用的公钥,具体获取方式 : 获取应用公钥和私钥 a. 应用私钥和支付宝公钥 获取 ...

  5. Spring入门(三)——AOP

    1. AOP aspect object programming ,简单来说就是把重复的代码抽取出来,然后再需要用到的地方进行切入,这里讲解基于接口的注解实现 2. 了解 关注点:即重复的代码 切面: ...

  6. webuploader+web如何实现分片+断点续传

    众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路. 实现文件夹 ...

  7. PPP

    名称 chat–调制解调器的自动对话脚本 命令格式 chat [options] script 描述 Chat程序定义了一个计算机和调制解调器之间对话交流,其主要目的是用来在本地PPPD和远端PPPD ...

  8. EL获取域中的数据

    EL(Expression Language)是表达式语言,EL的使用可以减少JAVA代码的书写. 1.EL表达式中的常量: <body bgcolor="#7fffd4"& ...

  9. 机器学习---用python实现朴素贝叶斯算法(Machine Learning Naive Bayes Algorithm Application)

    在<机器学习---朴素贝叶斯分类器(Machine Learning Naive Bayes Classifier)>一文中,我们介绍了朴素贝叶斯分类器的原理.现在,让我们来实践一下. 在 ...

  10. Day13:H5+JS+C3

    css布局中,什么是BFC BFC是Block formatting context的缩写,表示"块级格式化上下文". 设置BFC的元素,是一个独立的渲染区域,只有Block-le ...