使用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. uWSGI的各种部署方式

    As a back-end with web-server, speak the uwsgi protocol <uwsgi id = "uwsgibk"> <s ...

  2. 实现qsort(和qsort差一个数量级啊,伤自尊了)

    #include <cstdio> #include <cstdint> #include <ctime> #include <cstring> #in ...

  3. 云端中间层负载均衡工具 Eureka

    亚马逊提供了一个负载均衡工具 Elastic Load Balancer,但针对的是终端用户 Web 流量服务器,而 Eureka 针对的是中间层服务器的负载均衡.AWS 固有的环境,对 IP 地址. ...

  4. HIVE中的order by操作

    hive中常见的高级查询包括:group by.Order by.join.distribute by.sort by.cluster by.Union all.今天我们来看看order by操作,O ...

  5. ggplot2作图详解:入门函数qplot

    ggplot2作图详解:入门函数qplot   ggplot2的功能不用我们做广告,因为它的作者Hadley Wickham就说ggplot2是一个强大的作图工具,它可以让你不受现有图形类型的限制,创 ...

  6. WPF教程四:布局之DockPanel面板

    DockPanel:停靠面板 DockPanel定义一个区域,在此区域中,您可以使子元素通过描点的形式排列,这些对象位于 Children 属性中.停靠面板类似于WinForm中控件的Dock属性.D ...

  7. 移动H5功能设计反思 测试用例总结

    一.线上页面滑动流畅性测试 1.减少长动画效果(影响流畅) 2.是否自动跳转或者还是让用户自己操作跳转需要推敲 二.buttom和页面滑动的选择(优劣) 部分手机本身就会滑动不灵敏,大部分时候其实用b ...

  8. Java获取yahoo天气预报

    学习闲暇之余,写了个获取yahoo天气预报的java小程序,仅供娱乐. 首先我们需要获取您需要查询城市对应的代号,我们可以用HashMap来查询,代码如下: publicstatic HashMap& ...

  9. MongoDB状态查询:db.serverStatus()

    参见:http://www.2cto.com/database/201501/370191.html 基本信息 spock:PRIMARY>db.serverStatus() { "h ...

  10. Unity3D-光照贴图技术

    概念 Lightmapping光照贴图技术是一种增强静态场景光照效果的技术,其优点是可以通过较少的性能消耗使静态场景看上去更加真实,丰富,更加具有立体感:缺点是不能用来实时地处理动态光照.当游戏场景包 ...