从零开始···spring整合redis配置

来源:互联网 发布:linux删除用户组命令 编辑:程序博客网 时间:2024/06/05 17:20

想在spring中整合redis,首先是下载安装redis。
redis官方版本是linux环境下的,但是有一个微软维护的版本是适用于windows环境的。可以去github上下载:https://github.com/MicrosoftArchive/redis
下载压缩包后解压,然后在dos命令窗口中,在解压目录下键入redis-server.exe就可以启动redis
启动redis
另启一个窗口,键入redis-cli.exe -h 127.0.0.1 -p 6379可以开始使用redis
使用
(图中因为已经配置了环境变量,无需转到文件目录下)


接下来就是redis的配置:
1.依赖包

<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->    <dependency>        <groupId>redis.clients</groupId>        <artifactId>jedis</artifactId>        <version>2.9.0</version>    </dependency>    <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->    <dependency>        <groupId>org.springframework.data</groupId>        <artifactId>spring-data-redis</artifactId>        <version>1.7.2.RELEASE</version>    </dependency>

这里需要注意版本的选择,spring-data-redis开始选择的是2.0版本,但是启动后报错,查询发现可能是jar包的冲突。后来换了旧一点的版本就好了。
2.配置文件
application-reids.xml

<!-- 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 >

然后是redis.properties

redis.host=127.0.0.1redis.port=6379  redis.password=  redis.maxIdle=100  redis.maxActive=300  redis.maxWait=1000  redis.testOnBorrow=true  redis.timeout=100000

这里没有密码,所以空着。连接的是本地的redis,所以host是127.0.0.1。
3.工具类
建立工具类,调用RedisTemplate实现相关的数据操作。

@Componentpublic final class RedisUtil {    @Autowired    private RedisTemplate<Serializable, Object> redisTemplate;    /**     * 写入缓存     *      * @param key     * @param value     * @return     */    public boolean set(final String key, Object value) {        boolean result = false;        try {            ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();            operations.set(key, value);            result = true;        } catch (Exception e) {            e.printStackTrace();        }        return result;    }    /**     * 读取缓存     *      * @param key     * @return     */    public Object get(final String key) {        Object result = null;        ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();        result = operations.get(key);        return result;    }}

然后就可以在程序中调用工具类实现redis的存取操作。

原创粉丝点击