Redis的安裝、部署和與Spring Cache整合

來源:互聯網
上載者:User

標籤:redis

安裝

去http://redis.io/下載最新穩定版的源碼。解壓,進入解壓目錄,執行

makemake install

之後在src/目錄下會多出以下幾個檔案:

redis-serverredis-benchmarkredis-cliredis-conf

將其copy/usr/redis目錄下即可。

部署

修改redis-conf檔案,添加:

requirepass 111111

將串連密碼設為111111。然後執行

./redis-server redis-conf

即可啟動redis伺服器。

與Spring Cache整合

首先在pom.xml中添加jedis和spring data redis依賴:

<!-- Spring Data Redis -->        <dependency>            <groupId>org.springframework.data</groupId>            <artifactId>spring-data-redis</artifactId>            <version>${spring-redis}</version>        </dependency>        <dependency>            <groupId>redis.clients</groupId>            <artifactId>jedis</artifactId>            <version>${jedis}</version>        </dependency>

因為我們將要使用jackson提供class序列化功能,因此還需要添加:

<dependency>            <groupId>org.codehaus.jackson</groupId>            <artifactId>jackson-mapper-asl</artifactId>            <version>1.9.4</version>        </dependency>

然後,在spring設定檔中添加:

<!-- Redis緩衝配置 -->     <beans:bean id="redisConnectionFactory"                class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >        <beans:property name="hostName" value="XX.XX.XX.XX" />        <beans:property name="password" value="111111" />    </beans:bean>    <!-- Serializer -->    <beans:bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />    <beans:bean id="valueSerializer" class="org.springframework.data.redis.serializer.JacksonJsonRedisSerializer" />    <!-- redis template -->    <beans:bean id="redisTemplate"                class="org.springframework.data.redis.core.StringRedisTemplate">        <beans:property name="connectionFactory" ref="redisConnectionFactory" />        <beans:property name="keySerializer" ref="keySerializer" />        <beans:property name="valueSerializer" ref="valueSerializer" />    </beans:bean>    <!-- cache manager -->    <beans:bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">        <beans:constructor-arg name="template" ref="redisTemplate" />    </beans:bean>

完成以上工作後,就可以在service方法中使用@Cacheable方法了:

 @Override    @Transactional(readOnly = true)    @Cacheable(value = "cache", key = "#username")    public MemberModel findMember(String username, boolean isWired) {        MemberModel mem = memMapper.selectByUsername(username);        CheckUtils.nullCheck(mem);        return mem;    }

但是使用spring cache有個缺點,就是無法設定cache到期時間。如果有這方面需求,就必須直接使用jedis用戶端而不是spring cache了。

Redis的安裝、部署和與Spring Cache整合

聯繫我們

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