栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 数据库 > 缓存机制 > Redis缓存

windows环境下Redis+Spring缓存实例讲解

Redis缓存 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

windows环境下Redis+Spring缓存实例讲解

一、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 FilesRedis),然后如下命令开启:

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
 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 < arguments.length; i++) {
    sbu.append("_").append(arguments[i]);
   }
  }
  return sbu.toString();
 }
 
 
 public boolean exists(final String key) {
  return redisTemplate.hasKey(key);
 }
 
 
 public Object getCache(final String key) {
  Object result = null;
  ValueOperations operations = redisTemplate
    .opsForValue();
  result = operations.get(key);
  return result;
 }
 
 
 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

以上就是本文的全部内容,希望对大家的学习有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/173662.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号