windows環境下Redis+Spring緩衝執行個體講解_Redis

來源:互聯網
上載者:User

一、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:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.luo</groupId> <artifactId>redis_project</artifactId> <version>0.0.1-SNAPSHOT</version>  <properties>  <!-- spring版本號碼 -->  <spring.version>3.2.8.RELEASE</spring.version>  <!-- junit版本號碼 -->  <junit.version>4.10</junit.version> </properties>  <dependencies>  <!-- 添加Spring依賴 -->  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-core</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-webmvc</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-context-support</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-aop</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-aspects</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-tx</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-jdbc</artifactId>   <version>${spring.version}</version>  </dependency>  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-web</artifactId>   <version>${spring.version}</version>  </dependency>   <!--單元測試依賴 -->  <dependency>   <groupId>junit</groupId>   <artifactId>junit</artifactId>   <version>${junit.version}</version>   <scope>test</scope>  </dependency>   <!--spring單元測試依賴 -->  <dependency>   <groupId>org.springframework</groupId>   <artifactId>spring-test</artifactId>   <version>${spring.version}</version>   <scope>test</scope>  </dependency>   <!-- Redis 相關依賴 -->  <dependency>   <groupId>org.springframework.data</groupId>   <artifactId>spring-data-redis</artifactId>   <version>1.6.1.RELEASE</version>  </dependency>  <dependency>   <groupId>redis.clients</groupId>   <artifactId>jedis</artifactId>   <version>2.7.3</version>  </dependency>  </dependencies></project>

3.2、spring設定檔application.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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="  http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd">  <!-- 自動掃描註解的bean --> <context:component-scan base-package="com.luo.service" />  <!-- 引入properties設定檔 -->  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  <property name="locations">   <list>    <value>classpath:properties/*.properties</value>    <!--要是有多個設定檔,只需在這裡繼續添加即可 -->   </list>  </property> </bean>  <!-- jedis 配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig" >   <property name="maxIdle" value="${redis.maxIdle}" />   <property name="maxWaitMillis" value="${redis.maxWait}" />   <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean >  <!-- redis伺服器中心 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >   <property name="poolConfig" ref="poolConfig" />   <property name="port" value="${redis.port}" />   <property name="hostName" value="${redis.host}" />   <!-- <property name="password" value="${redis.password}" /> -->   <property name="timeout" value="${redis.timeout}" ></property> </bean > <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" >   <property name="connectionFactory" ref="connectionFactory" />   <property name="keySerializer" >    <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />   </property>   <property name="valueSerializer" >    <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />   </property> </bean >  <!-- cache配置 --> <bean id="methodCacheInterceptor" class="com.luo.redis.cache.MethodCacheInterceptor" >   <property name="redisTemplate" ref="redisTemplate" /> </bean >  <!-- aop配置切點跟通知 --> <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  <property name="advice" ref="methodCacheInterceptor"/>  <property name="pattern" value=".*ServiceImpl.*getTimestamp"/> </bean> <bean id="redisTestService" class="com.luo.service.impl.RedisTestServiceImpl"> </bean> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> </beans>

3.3、Redis配置參數,redis.properties:

#redis中心#綁定的主機地址redis.host=127.0.0.1#指定Redis監聽連接埠,預設連接埠為6379redis.port=6379#授權密碼(本例子沒有使用)redis.password=123456 #最大空閑數:空閑連結數大於maxIdle時,將進行回收redis.maxIdle=100 #最大串連數:能夠同時建立的“最大連結個數”redis.maxActive=300 #最大等待時間:單位msredis.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; @Servicepublic 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<Serializable, Object> 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 < arguments.length; i++) {    sbu.append("_").append(arguments[i]);   }  }  return sbu.toString(); }  /**  * 判斷緩衝中是否有對應的value  *   * @param key  * @return  */ public boolean exists(final String key) {  return redisTemplate.hasKey(key); }  /**  * 讀取緩衝  *   * @param key  * @return  */ public Object getCache(final String key) {  Object result = null;  ValueOperations<Serializable, Object> 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<Serializable, Object> 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<Serializable, Object> 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

以上就是本文的全部內容,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.