使用spring-data-redis与redis集成,今天我们就通过例子来学习一下。当时间和耐心都已经变为奢侈,我们只能靠星座了解彼此。

spring与redis集成的实例

注意:这里我们测试的是安装在window本地的redis,版本为redis-3-2.0。测试的项目结构如下,主体有三个文件。

一、在pom.xml中添加spring-data-redis的依赖

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency> <dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>

二、定义一个主的配置文件spring-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:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
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"> <context:property-placeholder location="classpath:redis.properties"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxTotal}" />
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
<property name="testOnBorrow" value="true" />
</bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="${redis.host}"
p:port="${redis.port}"
p:pool-config-ref="jedisPoolConfig"/> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>

三、一些属性的配置,放在redis.properties文件中

# Redis settings
redis.host=localhost
redis.port= redis.maxIdle=
redis.maxTotal=
redis.maxWaitMillis=

这里面用到的jedis版本比较新,网上比较旧的配置属性是不一样的。

配置文件中千万要注意前后的空格问题,如果localhost有空格,那么会报找不到host的。而且这个错有点难以发现,关于空格的问题要注意一下。

四、主体测试类RedisMain.java

我们这里简单的演示一下,string数据的插入与取得过程。

package com.linux.huhx.springRedis;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer; /**
* Created by huhx on 2017-07-18.
*/
public class RedisMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-redis.xml");
final RedisTemplate redisTemplate = (StringRedisTemplate) context.getBean("redisTemplate"); // 存放key-value
redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] key = serializer.serialize("username");
byte[] value = serializer.serialize("liuling");
return connection.setNX(key, value);
}
}); // 得到key,得到value
redisTemplate.execute(new RedisCallback<Boolean>() {
@Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
RedisSerializer<String> serializer = redisTemplate.getStringSerializer();
byte[] key = serializer.serialize("username");
String username = serializer.deserialize(connection.get(key));
System.out.println(username); // liuling
return null;
}
});
}
}

五、在本地启动redis,运行RedisMain程序。

在redis-cli的控制台,可以看出已经成功插入数据到redis中。

友情链接

springdata----->spring集成redis(一)的更多相关文章

  1. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  2. spring 集成 redis -- pub/sub

    redis除了常用的当做缓存外,还可以当做简单的消息中间件,实现消息发布订阅 spring集成redis,可以使用spring-data-redis 首先引入相关maven依赖(此处我spring相关 ...

  3. spring集成redis

    redis是一种非关系型数据库,与mongoDB不同的是redis是内存数据库,所以访问速度很快.常用作缓存和发布-订阅式的消息队列.redis官方没有提供windows版本的软件.windows版本 ...

  4. spring 集成redis客户端jedis(java)

    spring集成jedis简单实例   jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...

  5. spring集成redis,集成redis集群

    原文:http://chentian114.iteye.com/blog/2292323 1.通过spring-data-redis集成redis pom.xml依赖包 <project xml ...

  6. Spring集成Redis方案(spring-data-redis)(基于Jedis的单机模式)(待实践)

    说明:请注意Spring Data Redis的版本以及Spring的版本!最新版本的Spring Data Redis已经去除Jedis的依赖包,需要自行引入,这个是个坑点.并且会与一些低版本的Sp ...

  7. spring集成redis——主从配置以及哨兵监控

    Redis主从模式配置: Redis的主从模式配置是非常简单的,首先我们需要有2个可运行的redis环境: master node : 192.168.56.101 8887 slave node: ...

  8. Spring集成Redis缓存

    作者:13 GItHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...

  9. Spring+SpringMVC+MyBatis+easyUI整合进阶篇(十二)Spring集成Redis缓存

    作者:13 GitHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...

  10. Spring集成Redis使用注解

    转载:http://blog.csdn.net/u013725455/article/details/52129283 使用Maven项目,添加jar文件依赖: <project xmlns=& ...

随机推荐

  1. phalcon的CLI应用

    CLI应用是命令行下执行的程序, 可以应用于定时任务,守护进程, 脚本, 公用命令等等. 最小的目录结构:app/config/config.phpapp/tasks/MainTask.phpapp/ ...

  2. CheckBoxList 全选(jquery版本)

    function selectedAll(allselect, obj) { $("#"+obj.id+" input:checkbox").each(func ...

  3. FastCGI 进程管理器(FPM)

    FPM(FastCGI 进程管理器)用于替换 PHP FastCGI 的大部分附加功能,对于高负载网站是非常有用的. 它的功能包括: 支持平滑停止/启动的高级进程管理功能: 可以工作于不同的 uid/ ...

  4. YARN : Architecture of Next Generation Apache Hadoop MapReduceFramework

    转自:http://blog.csdn.net/colorant/article/details/9146201 == 目标问题 == 下一代的Hadoop框架,支持10,000+节点规模的Hadoo ...

  5. Photoshop脚本入门

  6. MyEclipse实现xml的自动提示

    每次出现不能自动提示,蛮烦的.虽然不是一个很难的问题,但是有时候就是记得这个很简单的几步,所以记录下来以备用. 现在mybatis主要是3版本,即此时根据版本3来写的,别的都一样. 1,下载dtd文件 ...

  7. linux -- Linux下的五个查找命令:grep、find、locate、whereis、which

    1.grep grep(General Regular Expression Parser,通用规则表达式分析程序)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 它的使 ...

  8. 节日(CCF试题)

    试题编号:    201503-3试题名称:    节日时间限制:    1.0s内存限制:    256.0MB问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比 ...

  9. iOS 图片切片的简单实现

    图片切片就是将一张图片按一定比例切分,中间部分系统自动填充空白, 这样在文本多行输入的时候,将具有特定形状的图片作为背景, 即使文本行数很多,也不会使图片走形. //即使你按5/5分,还是3/7分系统 ...

  10. Hadoop参数汇总

    linux参数 以下参数最好优化一下: 文件描述符ulimit -n 用户最大进程 nproc (hbase需要 hbse book) 关闭swap分区 设置合理的预读取缓冲区 Linux的内核的IO ...