基於spring架構的redis緩衝

來源:互聯網
上載者:User

標籤:alibaba   let   jar包   public   sof   ESS   hub   fastjson   ble   

前言:redis使用的非常廣泛,其優點是速度快、支援豐富的資料類型、支援事物操作等,適用於會話緩衝(session cache)、全頁緩衝(FPC)、隊列等,極大的減少了資料庫的負擔。

1.安裝下載redis

   網址:https://github.com/MicrosoftArchive/redis/releases,安裝成功後,開啟redis服務。

2.匯入jar包

   commons-pool.jar  jedis.jar spring-data-redis.jar fastjson.jar  aopalliance.jar

 注意:jedis和Commons-pool兩個jar包的版本是有對應關係的,注意引用的時候要配對使用,否則將報錯。

3.配置redis檔案

redis.properties:

redis.hostName=127.0.0.1redis.port=6379redis.timeout=15000redis.usePool=trueredis.maxTotal=32redis.maxIdle=20redis.minIdle=10redis.minEvictableIdleTimeMillis=300000redis.numTestsPerEvictionRun=3redis.timeBetweenEvictionRunsMillis=60000

spring-redis.xml:

   <context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>        <!-- redis 單點配置 -->    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">        <property name="maxTotal" value="${redis.maxTotal}"></property>        <property name="maxIdle" value="${redis.maxIdle}"></property>        <property name="minIdle" value="${redis.minIdle}"></property>        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}"></property>        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}"></property>        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}"></property>        <property name="testOnBorrow" value="true"></property>    </bean>           <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">        <property name="database" value="5"></property>        <property name="poolConfig" ref="jedisPoolConfig"></property>        <property name="hostName" value="${redis.hostName}"></property>        <property name="port" value="${redis.port}"></property>        <property name="timeout" value="${redis.timeout}"></property>        <property name="usePool" value="true"></property>    </bean>    <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">        <property name="connectionFactory" ref="jedisConnectionFactory"></property>    </bean>    <bean id="redisUtil" class="com.util.RedisUtil">        <property name="redisTemplate" ref="jedisTemplate"></property>    </bean>

4.在spring的主設定檔中填入以下資訊:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />        <property name="ignoreResourceNotFound" value="true" />        <property name="locations">            <list>                <value>classpath:redis.properties</value>            </list>        </property>    </bean><import resource="spring-redis.xml" />

5.建立工具類(RedisUtil)

package com.util;import java.io.UnsupportedEncodingException;import java.util.List;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;/** *  * <p>Title: Redis操作類</p> * <p>Description: redis操作的基本方法</p> * <p>Company: yangcnye</p> */public class RedisUtil<T> {    private final static String redisCode = "utf-8";    protected RedisTemplate<String, T> redisTemplate;        public RedisTemplate<String, T> getRedisTemplate() {        return redisTemplate;    }    public void setRedisTemplate(RedisTemplate<String, T> redisTemplate) {        this.redisTemplate = redisTemplate;    }       /**     * 字串添加     * @param key     * @param value     * @param liveTime     */    public void set(String key, String value, long liveTime) {        try {            this.set(key.getBytes(redisCode), value.getBytes(redisCode), liveTime);        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }    }}

5.測試緩衝是否成功,建立個controller

package com.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import com.alibaba.fastjson.JSON;import com.util.RedisUtil;/*** * redis緩衝運用 *  * @author yangcnye * */@Controller@RequestMapping("/redis")public class RedisController {    private static int i = 1;    private static int j = 1;    private static int k = 0;    private static final int l = 0;    @Autowired    private RedisUtil<Object> redis;    @RequestMapping(value = "/RedisTest", method = RequestMethod.POST)    public String RedisTest(@RequestParam("description") String description) {        System.out.println(description);        redis.set("num", JSON.toJSONString(description), 6000);        return "success";    }}

以上就是基於spring項目中的redis簡單應用,後續補充,歡迎大家交流。

                            

基於spring架構的redis緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.