windows环境下Redis+Spring缓存实例讲解
本篇文章给大家分享《windows环境下Redis+Spring缓存实例讲解》,覆盖了数据库的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
一、Redis了解
1.1、Redis介绍:
redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set –有序集合)和hash(哈希类型)。这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的。在此基础上,redis支持各种不同方式的排序。与memcached一样,为了保证效率,数据都是缓存在内存中。区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步。
Redis数据库完全在内存中,使用磁盘仅用于持久性。相比许多键值数据存储,Redis拥有一套较为丰富的数据类型。Redis可以将数据复制到任意数量的从服务器。
1.2、Redis优点:
(1)异常快速:Redis的速度非常快,每秒能执行约11万集合,每秒约81000+条记录。
(2)支持丰富的数据类型:Redis支持最大多数开发人员已经知道像列表,集合,有序集合,散列数据类型。这使得它非常容易解决各种各样的问题,因为我们知道哪些问题是可以处理通过它的数据类型更好。
(3)操作都是原子性:所有Redis操作是原子的,这保证了如果两个客户端同时访问的Redis服务器将获得更新后的值。
(4)多功能实用工具:Redis是一个多实用的工具,可以在多个用例如缓存,消息,队列使用(Redis原生支持发布/订阅),任何短暂的数据,应用程序,如Web应用程序会话,网页命中计数等。
1.3、Redis缺点:
(1)单线程
(2)耗内存
二、64位windows下Redis安装
Redis官方是不支持windows的,但是Microsoft Open Tech group 在 GitHub上开发了一个Win64的版本,下载地址:https://github.com/MSOpenTech/redis/releases。注意只支持64位哈。
小宝鸽是下载了Redis-x64-3.0.500.msi进行安装。安装过程中全部采取默认即可。
安装完成之后可能已经帮你开启了Redis对应的服务,博主的就是如此。查看资源管理如下,说明已经开启:

已经开启了对应服务的,我们让它保持,下面例子需要用到。如果没有开启的,我们命令开启,进入Redis的安装目录(博主的是C:\Program Files\Redis),然后如下命令开启:
redis-server redis.windows.conf

OK,下面我们进行实例。
三、详细实例
本工程采用的环境:Eclipse + maven + spring + junit
3.1、添加相关依赖(spring+junit+redis依赖),pom.xml:
4.0.0 com.luo redis_project 0.0.1-SNAPSHOT 3.2.8.RELEASE 4.10 org.springframework spring-core ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-aop ${spring.version} org.springframework spring-aspects ${spring.version} org.springframework spring-tx ${spring.version} org.springframework spring-jdbc ${spring.version} org.springframework spring-web ${spring.version} junit junit ${junit.version} test org.springframework spring-test ${spring.version} test org.springframework.data spring-data-redis 1.6.1.RELEASE redis.clients jedis 2.7.3
3.2、spring配置文件application.xml:
classpath:properties/*.properties
3.3、Redis配置参数,redis.properties:
#redis中心 #绑定的主机地址 redis.host=127.0.0.1 #指定Redis监听端口,默认端口为6379 redis.port=6379 #授权密码(本例子没有使用) redis.password=123456 #最大空闲数:空闲链接数大于maxIdle时,将进行回收 redis.maxIdle=100 #最大连接数:能够同时建立的“最大链接个数” redis.maxActive=300 #最大等待时间:单位ms redis.maxWait=1000 #使用连接时,检测连接是否成功 redis.testOnBorrow=true #当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能 redis.timeout=10000
3.4、添加接口及对应实现RedisTestService.Java和RedisTestServiceImpl.java:
package com.luo.service;
public interface RedisTestService {
public String getTimestamp(String param);
}
package com.luo.service.impl;
import org.springframework.stereotype.Service;
import com.luo.service.RedisTestService;
@Service
public class RedisTestServiceImpl implements RedisTestService {
public String getTimestamp(String param) {
Long timestamp = System.currentTimeMillis();
return timestamp.toString();
}
}
3.5、本例采用spring aop切面方式进行缓存,配置已在上面spring配置文件中,对应实现为MethodCacheInterceptor.java:
package com.luo.redis.cache;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
public class MethodCacheInterceptor implements MethodInterceptor {
private RedisTemplate redisTemplate;
private Long defaultCacheExpireTime = 10l; // 缓存默认的过期时间,这里设置了10秒
public Object invoke(MethodInvocation invocation) throws Throwable {
Object value = null;
String targetName = invocation.getThis().getClass().getName();
String methodName = invocation.getMethod().getName();
Object[] arguments = invocation.getArguments();
String key = getCacheKey(targetName, methodName, arguments);
try {
// 判断是否有缓存
if (exists(key)) {
return getCache(key);
}
// 写入缓存
value = invocation.proceed();
if (value != null) {
final String tkey = key;
final Object tvalue = value;
new Thread(new Runnable() {
public void run() {
setCache(tkey, tvalue, defaultCacheExpireTime);
}
}).start();
}
} catch (Exception e) {
e.printStackTrace();
if (value == null) {
return invocation.proceed();
}
}
return value;
}
/**
* 创建缓存key
*
* @param targetName
* @param methodName
* @param arguments
*/
private String getCacheKey(String targetName, String methodName,
Object[] arguments) {
StringBuffer sbu = new StringBuffer();
sbu.append(targetName).append("_").append(methodName);
if ((arguments != null) && (arguments.length != 0)) {
for (int i = 0; i operations = redisTemplate
.opsForValue();
result = operations.get(key);
return result;
}
/**
* 写入缓存
*
* @param key
* @param value
* @return
*/
public boolean setCache(final String key, Object value, Long expireTime) {
boolean result = false;
try {
ValueOperations operations = redisTemplate
.opsForValue();
operations.set(key, value);
redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
result = true;
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public void setRedisTemplate(
RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
}
3.6、单元测试相关类:
package com.luo.baseTest;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
//指定bean注入的配置文件
@ContextConfiguration(locations = { "classpath:application.xml" })
//使用标准的JUnit @RunWith注释来告诉JUnit使用Spring TestRunner
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTestCase extends AbstractJUnit4SpringContextTests {
}
package com.luo.service;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.luo.baseTest.SpringTestCase;
public class RedisTestServiceTest extends SpringTestCase {
@Autowired
private RedisTestService redisTestService;
@Test
public void getTimestampTest() throws InterruptedException{
System.out.println("第一次调用:" + redisTestService.getTimestamp("param"));
Thread.sleep(2000);
System.out.println("2秒之后调用:" + redisTestService.getTimestamp("param"));
Thread.sleep(11000);
System.out.println("再过11秒之后调用:" + redisTestService.getTimestamp("param"));
}
}
3.7、运行结果:

四、源码下载:redis-project(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助。
本篇关于《windows环境下Redis+Spring缓存实例讲解》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于数据库的相关知识,请关注golang学习网公众号!
win 7 安装redis服务【笔记】
- 上一篇
- win 7 安装redis服务【笔记】
- 下一篇
- Linux下安装Redis并设置相关服务
-
- 玩命的台灯
- 这篇文章内容太及时了,太细致了,感谢大佬分享,码住,关注大佬了!希望大佬能多写数据库相关的文章。
- 2023-03-10 03:27:29
-
- 笑点低的狗
- 很好,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,帮助很大,总算是懂了,感谢楼主分享技术文章!
- 2023-02-02 08:18:52
-
- 甜美的高山
- 太详细了,已收藏,感谢师傅的这篇技术文章,我会继续支持!
- 2023-02-02 00:30:25
-
- 生动的小鸭子
- 这篇技术文章太及时了,太全面了,太给力了,收藏了,关注作者了!希望作者能多写数据库相关的文章。
- 2023-01-04 22:23:01
-
- 数据库 · Redis | 3天前 |
- RedisLua脚本实现复杂正则匹配方法
- 438浏览 收藏
-
- 数据库 · Redis | 3天前 |
- Redis客户端缓冲区优化技巧
- 146浏览 收藏
-
- 数据库 · Redis | 3天前 |
- RedisPSUBSCRIBE耗CPU原因解析
- 476浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis分布式锁释放原子性保障方案
- 216浏览 收藏
-
- 数据库 · Redis | 4天前 |
- RedisLua脚本实现分布式事务补偿与回滚
- 180浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis6.0线程优化与CPU绑定方法
- 326浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis发布订阅支持消息压缩吗?
- 415浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis缓存优化:调整淘汰策略提命中率
- 242浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis集群节点负载查看技巧
- 369浏览 收藏
-
- 数据库 · Redis | 4天前 |
- Redis7.0IO多线程优化方法
- 251浏览 收藏
-
- 数据库 · Redis | 5天前 |
- Redis集群Pub/Sub如何减少广播消耗
- 451浏览 收藏
-
- 数据库 · Redis | 5天前 |
- Redis主从优化:延长repl-backlog-ttl设置
- 477浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- ChatExcel酷表
- ChatExcel酷表是由北京大学团队打造的Excel聊天机器人,用自然语言操控表格,简化数据处理,告别繁琐操作,提升工作效率!适用于学生、上班族及政府人员。
- 6006次使用
-
- Any绘本
- 探索Any绘本(anypicturebook.com/zh),一款开源免费的AI绘本创作工具,基于Google Gemini与Flux AI模型,让您轻松创作个性化绘本。适用于家庭、教育、创作等多种场景,零门槛,高自由度,技术透明,本地可控。
- 6426次使用
-
- 可赞AI
- 可赞AI,AI驱动的办公可视化智能工具,助您轻松实现文本与可视化元素高效转化。无论是智能文档生成、多格式文本解析,还是一键生成专业图表、脑图、知识卡片,可赞AI都能让信息处理更清晰高效。覆盖数据汇报、会议纪要、内容营销等全场景,大幅提升办公效率,降低专业门槛,是您提升工作效率的得力助手。
- 6236次使用
-
- 星月写作
- 星月写作是国内首款聚焦中文网络小说创作的AI辅助工具,解决网文作者从构思到变现的全流程痛点。AI扫榜、专属模板、全链路适配,助力新人快速上手,资深作者效率倍增。
- 8211次使用
-
- MagicLight
- MagicLight.ai是全球首款叙事驱动型AI动画视频创作平台,专注于解决从故事想法到完整动画的全流程痛点。它通过自研AI模型,保障角色、风格、场景高度一致性,让零动画经验者也能高效产出专业级叙事内容。广泛适用于独立创作者、动画工作室、教育机构及企业营销,助您轻松实现创意落地与商业化。
- 6825次使用
-
- 关于golangtest缓存问题
- 2023-01-01 298浏览
-
- 基于 Spring Aop 环绕通知实现 Redis 缓存双删功能(示例代码)
- 2022-12-31 334浏览
-
- Django使用Redis进行缓存详细步骤
- 2023-01-07 291浏览
-
- Go语言基于HTTP的内存缓存服务的实现
- 2022-12-24 388浏览
-
- MySQL+Redis缓存+Gearman共同构建数据库缓存的方法
- 2023-01-21 495浏览

