redis缓存---安装及注解的形式(3)

redis安装
1.地址 https://github.com/MSOpenTech/redis/releases
 redis-x64-3.2.100.zip
2.解压到c盘 重命名为redis
2.打开cmd进入安装路径 cd c:\redis
3.运行redis-server.exe redis.windows.conf启动redis服务器(不要关闭)
4.新开个cmd 进入安装路径 cd c:\redis 运行连接命令: redis-cli.exe -h 127.0.0.1 -p 6379
5. keys *  可以查看所有的缓存key
redis使用:http://www.runoob.com/redis/redis-install.html

 

pom.xml文件
<!-- redis 缓存 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>

       <dependency>
       <groupId>commons-logging</groupId>
          <artifactId>commons-logging</artifactId>
          <version>1.1.1</version>
          </dependency>

1.web.xml 中加载 spring-redis文件

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:config/spring/spring-application.xml,
            classpath:config/spring/spring-support.xml,
            classpath:config/spring/spring-mybatis.xml,
            classpath:config/spring/spring-redis.xml
        </param-value>
    </context-param>

2.spring-redis文件

<?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:p="http://www.springframework.org/schema/p"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xmlns:cache="http://www.springframework.org/schema/cache"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans      
                        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd      
                        http://www.springframework.org/schema/context      
                        http://www.springframework.org/schema/context/spring-context-4.2.xsd      
                        http://www.springframework.org/schema/mvc      
                        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd  
                        http://www.springframework.org/schema/cache   
                        http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">   


    <!--引入配置文件-->
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="order" value="1"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="locations">
            <list>
                <value>classpath:config/core/redis.properties</value>
            </list>
        </property>
    </bean>
      <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 -->    
     <cache:annotation-driven cache-manager="redisManager" />     
    
    <!--配置 jedis pool-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大连接数 -->
        <property name="maxTotal" value="${redis.pool.maxTotal}"/>
        <!-- 最大空闲时间 -->
        <property name="maxIdle" value="${redis.pool.maxIdle}"/>
        <!-- 每次最大连接数 -->
        <property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
        <!-- 释放扫描的扫描间隔 -->
        <property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
        <!-- 连接的最小空闲时间 -->
        <property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
        <!-- 连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放 -->
        <property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}"/>
        <!-- 获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1 -->
        <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"/>
        <!-- 在获得链接的时候检查有效性,默认false -->
        <property name="testOnBorrow" value="${redis.pool.testOnBorrow}"/>
        <!-- 在空闲时检查有效性,默认false -->
        <property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
        <!-- 连接耗尽时是否阻塞,false报异常,true阻塞超时 默认:true-->
        <property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}"/>
    </bean>
<!--spring data redis -->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.hostName}"/>
        <property name="port" value="${redis.port}"/>
        <property name="timeout" value="${redis.timeout}"/>
        <property name="database" value="${redis.dbIndex}"/>
        <property name="usePool" value="${redis.usePool}"/>
        <!--  <property name="password" value="${redis.pass}" /> -->
        <!--可以通过构造注入或者Set注入两种方式-->
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>

    <!--redisTemplate-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
    </bean>
    
     <!-- spring自己的缓存管理器,这里定义了缓存位置名称 ,即注解中的value -->    
      <bean id="redisManager" class="org.springframework.data.redis.cache.RedisCacheManager">
        <constructor-arg ref="redisTemplate"/>
       <!--  <property name="defaultExpiration" value="3000"/> -->
        <property name="transactionAware" value="true"/>
        <property name="usePrefix" value="true"/>
    </bean>
</beans>

 

3.redis.properties

 

#JedisPoolConfig的参数
#最大连接数
redis.pool.maxTotal=30
#最大空闲时间
redis.pool.maxIdle=10
#每次最大连接数
redis.pool.numTestsPerEvictionRun=1024
#释放扫描的扫描间隔
redis.pool.timeBetweenEvictionRunsMillis=30000
#连接的最小空闲时间
redis.pool.minEvictableIdleTimeMillis=1800000
#连接控歘按时间多久后释放,当空闲时间>该值且空闲连接>最大空闲连接数时直接释放
redis.pool.softMinEvictableIdleTimeMillis=10000
#获得链接时的最大等待毫秒数,小于0:阻塞不确定时间,默认-1
redis.pool.maxWaitMillis=1500
#在获得链接的时候检查有效性,默认false
redis.pool.testOnBorrow=true
#在空闲时检查有效性,默认false
redis.pool.testWhileIdle=true
#连接耗尽时是否阻塞,false报异常,true阻塞超时,默认true
redis.pool.blockWhenExhausted=false

#JedisConnectionFactory的参数
#主机地址,默认:localhost
redis.hostName=192.168.200.128
#主机端口,默认:6379
redis.port=6379
#超时时间,默认:2000
redis.timeout=3000
#密码
#redis.password
#是否使用连接池,默认true
redis.usePool=true
#使用数据库的索引,0-15之间的数字,默认:0
redis.dbIndex=0
#是否使用数据类型的转换,默认:true
#redis.convertPipelineAndTxResults
#哨兵配置
#redis.sentinelConfig
#集群配置
#redis.clusterConfig

 4.使用方式

(1)接口
public interface ApiFactoryService {
   
    PhoneUser findRecord(String key);
   
    void cleanCache(String
key);
}

(2)实现类

package com.api.service.ipml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.vinord.api.service.ApiFactoryService;


import com.vinord.smart.query.DoorVo;

@Service
public class ApiFactoryServiceIpml implements ApiFactoryService {
   
    @Override
    @Cacheable(value = "redis", key = "'token_'+#
key")
    public PhoneUser
findRecord(String key) {
        System.err.println("运行数据库。。");
        PhoneUser user = phoneUserRepository.findByName();
        return user;
    }

    @Override
    @CacheEvict(value = "redis", key = "'token_'+#
key")
    public void cleanCache(String key) {
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值