Redis整合spring总结
一:Redis简介:
Redis是一个开源(BSD许可)的内存数据结构存储,用作数据库,缓存和消息代理。
简单来说,它是一个以(key,value)的形式存储数据的数据库.
官网:https://redis.io/download 去下载对应的版本
二:使用流程:
1.解压:redis-2.4.5-win32-win64.zip
运行对应的系统版本(32bit/64bit)中的redis-server.exe和redis-cli.exe文件来启动服务和输入相应的操作.
2.图形化界面jedis下载网址:https://github.com/xetorthio/jedis。
3.Spring Data Redis 的使用介绍 官网:http://projects.spring.io/spring-data-redis/
3.1引入相关的jar包文件
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
3.2配置applicationContext.xml中的redisTemplate
<!-- 扫描redis的服务类 -->
<context:component-scan base-package="cn.itcast.redis.service" />
<!-- spring管理redis缓存管理器 -->
<bean id="redisCacheManager"
class="org.springframework.data.redis.cache.RedisCacheManager">
<constructor-arg index="0" ref="redisTemplate" />
</bean>
<cache:annotation-driven cache-manager="redisCacheManager"/>
<!-- jedis连接池 -->
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="300" />
<property name="maxWaitMillis" value="3000" />
<property name="testOnBorrow" value="true" />
</bean>
<!-- jedis连接工厂 -->
<bean id="connectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:host-name="localhost" p:port="6379" p:pool-config-ref="poolConfig"
p:database="0" />
<!-- spring-data-redis提供模板 -->
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="keySerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
</property>
<property name="valueSerializer">
<bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer">
</bean>
</property>
</bean>
3.3发送邮件保存激活码24H到redis中
//在action类中注入RedisTemplate<String,String>
@Autowired
private RedisTemplate<String,String> redisTemplate;
// 生成激活码
String activecode = RandomStringUtils.randomNumeric(32);
// 将激活码保存到redis,设置24小时失效
redisTemplate.opsForValue().set(model.getTelephone(), activecode, 24,
TimeUnit.HOURS);
3.4判断激活码是否有效
// 属性驱动
private String activecode; public void setActivecode(String activecode) {
this.activecode = activecode;
}
@Action("customer_activeMail")
public String activeMail() throws IOException {
ServletActionContext.getResponse().setContentType(
"text/html;charset=utf-8");
// 判断激活码是否有效
String activecodeRedis = redisTemplate.opsForValue().get(
model.getTelephone());
if (activecodeRedis == null || !activecodeRedis.equals(activecodeRedis)) {
// 激活码无效
ServletActionContext.getResponse().getWriter()
.println("激活码无效,请登录系统,重新绑定邮箱!");
} else {
// 激活码有效
// 防止重复绑定
// 调用CRM webService 查询客户信息,判断是否已经绑定
Customer customer = WebClient
.create("http://localhost:9002/crm_management/services"
+ "/customerService/customer/telephone/"
+ model.getTelephone())
.accept(MediaType.APPLICATION_JSON).get(Customer.class);
if (customer.getType() == null || customer.getType() != 1) {
// 没有绑定,进行绑定
WebClient.create(
"http://localhost:9002/crm_management/services"
+ "/customerService/customer/updatetype/"
+ model.getTelephone()).get();
ServletActionContext.getResponse().getWriter()
.println("邮箱绑定成功!");
} else {
// 已经绑定过
ServletActionContext.getResponse().getWriter()
.println("邮箱已经绑定过,无需重复绑定!");
} // 删除redis的激活码
redisTemplate.delete(model.getTelephone());
}
return NONE;
}
3.5编写服务器类实现对应的方法(结合WebService技术实现数据的查询与操作)
编写实体类(@XmlRootElement)-->服务类的接口
(@Path&@Get/@Post/@Push/@Delete/)-->实现类-->Dao
@Produces({ "application/xml", "application/json" })/
@Consumes({ "application/xml", "application/json" })/
Redis整合spring总结的更多相关文章
- Redis windows环境安装 以及 redis整合spring
Redis对于Linux是官方支持的,安装和使用没有什么好说的,普通使用按照官方指导,5分钟以内就能搞定.详情请参考: http://redis.io/download Redis官方是不支持wind ...
- redis整合Spring入门
首先 衷心感谢这篇博客给我入门时的启发 三颗心脏 你需要知道,spring的官方文档中已经注明,与redis整合时,spring的jar包版本不能低于4.2.6,否则不支持,会报错的哟 测试的时候请 ...
- Redis整合Spring实现分布式锁
spring把专门的数据操作独立封装在spring-data系列中,spring-data-redis是对Redis的封装 <dependencies> <!-- 添加spring- ...
- Redis整合Spring结合使用缓存实例
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
- redis整合spring @Bean写法
jedis是一款java连接redis的客户端,spring基于jedis进行了封装,提供了简洁的操作redis的方法. 使用maven进行管理jar包之间的依赖: <dependency> ...
- Redis整合Spring结合使用缓存实例(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的 ...
- redis整合Spring之序列化对象与反序列化
写在最前面 1.Spring必须是4.2.6及以上版本才支持redis 2.jar包版本建议统一 需要准备jar包 1.aopalliance-1.0.jar 2.spring-data-common ...
- Redis学习总结(3)——Redis整合Spring结合使用缓存实例
摘要:本文介绍了如何在Spring中配置redis,并通过Spring中AOP的思想,将缓存的方法切入到有需要进入缓存的类或方法前面. 一.Redis介绍 什么是Redis? redis是一个key- ...
- Redis整合Spring结合使用缓存实例(三)
一.Redis介绍 什么是Redis? redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set( ...
随机推荐
- python---Numpy模块中数组运算的常用代码示例
import numpy as np # Numpy数组操作 print('========访问列表元素, 切片,赋值===========') arr = np.array([2., 6., 5., ...
- 【OCP|052】iZ0-052最新题库及答案整理-第9题
9.Which is true about the Automatic Diagnostic Repository (ADR)? A) It includes diagnostic data for ...
- 剑指offer35题:第一个只出现一次的字符+剑指offer55题:字符流中第一个不重复的字符+剑指offer51题:数组中重复的数字
在看剑指offer的时候,感觉这三个题目很像,都是用哈希表可以解决,所以把这三个题整理出来,以供复习. 剑指offer35题:第一个只出现一次的字符 题目描述:在字符串中找出第一个只出现一次的字符.如 ...
- SQL语句insert into 不存在则插入,存在则修改
一 测试表的创建 -- ---------------------------- -- Table structure for User -- ---------------------------- ...
- [Objective-C语言教程]决策结构(10)
决策结构要求程序员指定一个或多个要由程序评估或测试的条件,以及在条件被确定为真时要执行的一个或多个语句,以及可选的,如果条件要执行的其他语句 被认定是假的. 以下是大多数编程语言中的典型决策结构的一般 ...
- String 源码浅析(一)
前言 相信作为 JAVAER,平时编码时使用最多的必然是 String 字符串,而相信应该存在不少人对于 String 的 api 很熟悉了,但没有看过其源码实现,其实我个人觉得对于 api 的使用, ...
- char * 与char []探究理解
问题引入 以前一直认为二者是一样的,今天突然发现他们还是有很大的不同的.例如char *a = "abc"和char b[] = "abc",当我使用strca ...
- Ubuntu下Nginx安装
1.1 安装Nginx $sudo apt-get install nginx Ubuntu安装之后的文件结构大致为: 所有的配置文件都在/etc/nginx下,并且每个虚拟主机已经安排在了/etc/ ...
- StringBuffer与StringBuilder的区别比较
关于AbstractStringBuilder 首先通过查看源码发现,StringBuffer与StringBuilder都继承自AbstractStringBuilder抽象类.而AbstractS ...
- json处理工具类
需要的jar包 <!-- Jackson Json处理工具包 --><dependency><groupId>com.fasterxml.jackson.core& ...