spring-data-redis --简单的用spring-data-redis
spring-data-redis序列化策略
spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷。sdr提供了4种内置的serializer:
- JdkSerializationRedisSerializer:使用JDK的序列化手段(serializable接口,ObjectInputStrean,ObjectOutputStream),数据以字节流存储,jdk序列化和反序列化数据
- StringRedisSerializer:字符串编码,数据以string存储
- JacksonJsonRedisSerializer:json格式存储
- OxmSerializer:xml格式存储
其中JdkSerializationRedisSerializer和StringRedisSerializer是最基础的序列化策略,其中“JacksonJsonRedisSerializer”与“OxmSerializer”都是基于stirng存储,因此它们是较为“高级”的序列化(最终还是使用string解析以及构建java对象)。
RedisTemplate中需要声明4种serializer,默认为“JdkSerializationRedisSerializer”:
1) keySerializer :对于普通K-V操作时,key采取的序列化策略
2) valueSerializer:value采取的序列化策略
3) hashKeySerializer: 在hash数据结构中,hash-key的序列化策略
4) hashValueSerializer:hash-value的序列化策略
无论如何,建议key/hashKey采用StringRedisSerializer。 这redis服务端用命令行好查看 配置如下
1、maven配置
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.4.1.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.0</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
2、spring xml配置文件
配置jedis的缓冲池
<!-- 配置Jedis的 缓冲池 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" p:maxTotal="32" p:maxIdle="6"
p:maxWaitMillis="15000">
</bean>
配置ConnectionFactory
<!-- 配置Jedis connection -->
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:poolConfig-ref="jedisPoolConfig" p:hostName="127.0.0.1" p:port="6379" p:usePool="true">
</bean>
创建RedisTemplate
<!-- 配置 redisTemplate 利用Stringserializer -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connectionFactory-ref="connectionFactory">
<property name="defaultSerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<!-- 使用string主要是key 在redis端用命令好读 不然默认的序列化没办法读 -->
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
<property name="hashKeySerializer">
<bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
</property>
</bean>
3、案例
private static ApplicationContext context = null;
private static RedisTemplate redisTemplate=null;
@org.junit.BeforeClass
public static void BeforeClass() {
if (context == null) {
System.out.println("ctx inited....");
context = new ClassPathXmlApplicationContext("spring-context.xml");
redisTemplate = context.getBean("redisTemplate",RedisTemplate.class);
}
}
1.String
@Test
public void testString(){
ValueOperations opsForValue = redisTemplate.opsForValue(); opsForValue.set("string:name", "achuan");
opsForValue.set("string:id","1"); Object name = opsForValue.get("string:name");
assertNotNull(name);
assertEquals("achuan", name.toString());
}
2.List
说到List Redis 提供了类似于消息队列的操作,
@Test
public void testList() {
ListOperations opsForList = redisTemplate.opsForList(); String keyName = "ListKey"; redisTemplate.delete(keyName); opsForList.leftPush(keyName, "zhangsan");
opsForList.leftPush(keyName, "lisi");
opsForList.leftPushAll(keyName, "wangwu", "zhaoliu", "qianqi"); Long size = opsForList.size(keyName);
System.out.println("size:" + size); for (long i = 0; i < size; i++) {
Object value = opsForList.index(keyName,i);
System.out.println("i:"+i+",value:"+value.toString());
} Object leftPop = opsForList.leftPop(keyName);
System.out.println("after pop size:"+opsForList.size(keyName));
}
3 Hashes (SETS 自己看看吧。。)
今天就先到这里,Redis 还有很多东西,比如cache transcation 等等 看下目录结构
spring-data-redis --简单的用spring-data-redis的更多相关文章
- python的redis简单使用
安装的Python版本 3.6.1 redis安装的2.8 安装redis模块 pip install redis 简单使用 redis-test.py import redis r=redis.Re ...
- JAVA入门[20]-Spring Data JPA简单示例
Spring 对 JPA 的支持已经非常强大,开发者只需关心核心业务逻辑的实现代码,无需过多关注 EntityManager 的创建.事务处理等 JPA 相关的处理.Spring Data JPA更是 ...
- 简单使用:spring boot整合spring Data JPA
JPA顾名思义就是Java Persistence API的意思,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中. 1.jpa具有什么优势? (1).标准 ...
- spring与redis简单整合
项目结构 整合需要的依赖 <dependencies> <dependency> <groupId>org.springframework</groupId& ...
- Spring和redis简单测试demo
1.1 首先创建一个Maven工程 File --> New --> Other,然后选择Maven目录下的Maven Project,如下图: 然后在弹出的面板中选择配置后,下一步即可, ...
- 63.JPA/Hibernate/Spring Data概念【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 事情的起源,无意当中在一个群里看到这么一句描述:"有人么?默默的问一句,现在开发用mybatis还是hibernate还是jpa&quo ...
- Spring + Redis ( 简单使用)
1.Redis 的 Java API Java 中 使用 Redis 工具,要先去 maven 仓库中,下载 jedis jar包 jedis 依赖 <dependency> <gr ...
- Spring学习笔记(八)Spring Data JPA学习
jpa简单的命名规则如下,这个不多做介绍,放在这里也是给自己以后查找起来方便,这篇文章主要介绍之前一直忽略了的几个点,像@NoRepositoryBean这个注解,以及怎么自定义Repositor ...
- spring boot系列(五)spring boot 配置spring data jpa (查询方法)
接着上面spring boot系列(四)spring boot 配置spring data jpa 保存修改方法继续做查询的测试: 1 创建UserInfo实体类,代码和https://www.cnb ...
- spring boot:用redis+lua实现基于ip地址的分布式流量限制(限流/简单计数器算法)(spring boot 2.2.0)
一,限流有哪些环节? 1,为什么要限流? 目的:通过对并发请求进行限速或者一个时间单位内的的请求进行限速,目的是保护系统可正常提供服务,避免被压力太大无法响应服务. 如果达到限制速率则可以采取预定的处 ...
随机推荐
- swift-08-使用键值对儿统计字符在字符串中出现的次数
// // main.swift // 12- // // Created by wanghy on 15/8/9. // Copyright (c) 2015年 wanghy. All ri ...
- 国庆第六日(2014年10月6日11:51:15),node-webkit,理财产品
(1)node-webkit:一篇很好的入门文章.入门.系列. 在window下的打包和运行.大漠的一篇讲解文章 . (2)lighttable: 官网. (3)现在的理财产品,雨后春笋般冒出:宝点网 ...
- LAMP 环境 快速安装
(一)安装Apache 1.下载安装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 yum install zlib-devel -y wget http://m ...
- Debian vim没有颜色的解决办法
最近在研究Linux kali 3.12-kali1-amd64 Debian 3.12.6-2kali1 x86_64 GNU/Linux Debian的内核 发现vim竟然没有颜色,root或 ...
- WORDPRESS插件开发学习(一)HELLO WORLD
WORDPRESS插件开发学习系列文章第一篇,在每篇文章的后面追加固定的字符“Hello World” 一.打开wordpress目录->wp-content->plugins 二.在pl ...
- 【python】【转】if else 和 elif
else和elif语句也可以叫做子句,因为它们不能独立使用,两者都是出现在if.for.while语句内部的.else子句可以增加一种选择:而elif子句则是需要检查更多条件时会被使用,与if和els ...
- mongodb篇二:mongodb克隆远程数据库,去重查询的命令及对应java语句
http://blog.csdn.net/qkxh320/article/details/16115671 1.首先操作mongodb最基本命令:: show databases; ...
- maya2105 - windows8 - numpy/scipy
To compile numpy, create a site.cfg file in numpy's source directory with the following or similar c ...
- .net线程同步
大家都晓得.NET中线程同步有以下几种方式: 临界区(Critical Section).互斥量(Mutex).信号量(Semaphore).事件(Event) 1.临界区:通过对多线程的串行化来访问 ...
- shell编程的一些例子5
1.here文档 here文档允许我们调用一个交互式程序:可以从脚本程序中输出大量的文本,从而不必echo每行 例子1: #!/bin/bash cat<<!DATA! This is a ...