Windows下启用redis,使用Srping-redis做简单Java对象的存取

Redis官方并不支持windows平台,windows团队提供了开发环境的使用版本,使用默认配置,启动redis-server.exe即可。

使用目录中的配置文件需要在CMD下把配置文件名作为参数启动

启动后如图:可以看到读取的配置文件路径为/path/to/redis.conf,不在上述目录中

 

工程目录结构:

存取值测试

maven依赖 pom.xml, spring的其它依赖已省略:

<!-- 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.6.RELEASE</version>
        </dependency>

redis客户端配置

# Redis settings

redis.host=127.0.0.1
redis.port=6379
redis.pass=
  
redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
redis.testOnReturn=true

 

applicationContext.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.0.xsd">


    <context:component-scan base-package="com.von" />

    <context:property-placeholder location="classpath:redis.properties" />




    <bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="maxWaitMillis" value="${redis.maxActive}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />
    </bean>

    <bean id="connectionFactoryJedis"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="${redis.host}"></property>
        <property name="port" value="${redis.port}"></property>
        <property name="password" value="${redis.pass}"></property>
        <property name="poolConfig" ref="redisPoolConfig"></property>
    </bean>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <constructor-arg ref="connectionFactoryJedis" />
    </bean>

    <bean id="jedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactoryJedis"></property>
        <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>

    <bean id="blogDAO" class="com.von.dao.redis.impl.BlogDaoImpl" />


</beans>

代码

package com.von.dao.redis;

import com.von.model.Blog;

public interface BlogDao {
    
    public Blog getBlog(long id);

    public void saveBlog(Blog bolg);
    

}
package com.von.dao.redis.impl;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
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;
import org.springframework.data.redis.serializer.RedisSerializer;

import com.von.dao.redis.BlogDao;
import com.von.model.Blog;

public class BlogDaoImpl implements BlogDao {

    @Autowired
    protected RedisTemplate<Serializable, Serializable> redisTemplate;

    public void saveBlog(final Blog blog) {
        redisTemplate.execute(new RedisCallback<Object>() {

            @SuppressWarnings({ "rawtypes", "unchecked" })
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                RedisSerializer redisSerializer = redisTemplate.getValueSerializer();
                redisSerializer.serialize(blog);
                connection.set(redisTemplate.getStringSerializer().serialize("blog.id." + blog.getId()),
                        redisSerializer.serialize(blog));
                // redisTemplate.getStringSerializer().serialize(blog.getContext()));
                return null;
            }
        });
    }

    public Blog getBlog(final long id) {
        return redisTemplate.execute(new RedisCallback<Blog>() {
            public Blog doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key = redisTemplate.getStringSerializer().serialize("blog.id." + id);
                if (connection.exists(key)) {
                    byte[] value = connection.get(key);
                    // redisTemplate.getStringSerializer().deserialize(value);
                    Blog blog;
                    @SuppressWarnings("rawtypes")
                    RedisSerializer redisSerializer = redisTemplate.getValueSerializer();
                    blog = (Blog) redisSerializer.deserialize(value);
                    // blog.setContext(context);
                    // blog.setId(id);
                    return blog;
                }
                return null;
            }
        });
    }

}

model Blog.java

对象序列化要求类集成 Serializable 接口,它就是一个标识,可以使用默认的1L;序列化和反序列化对象是要求这个标识一致,具体作用可查阅相关资料;例如:程序版本升级是可改变Serializable 值,已提醒低版本的类库进行升级。

package com.von.model;

import java.io.Serializable;

public class Blog implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private long id;

    private String context;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

}

 

测试代码:

package com.von.maintest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.von.dao.redis.BlogDao;
import com.von.model.Blog;

public class JedisTest {

    public static void main(String[] args) {
        ApplicationContext ac =  new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
        BlogDao blogDAO = (BlogDao)ac.getBean("blogDAO");
        Blog bolg1 = new Blog();
        bolg1.setId(3);
        bolg1.setContext("context-test-中文");
        blogDAO.saveBlog(bolg1);
        Blog blog2 = blogDAO.getBlog(3);
        System.out.println(blog2.getContext());

    }

}

 

posted on 2017-04-23 16:19  Dr.Von  阅读(1180)  评论(0)    收藏  举报

导航