spring-data-redis 自定义缓存(@annotation+lua+aop)(一)

介绍:使用spring-data-redis 框架 ,利用@annotation和redis lua,spring aop 实现数据的 save、list、update、delete对缓存的影响操作(考虑增加异步数据)

  save:数据库数据添加、缓存数据添加、list后的缓存数据添加

  list:缓存数据增加

  update:数据库数据更新、缓存数据更新、list后的缓存数据更新

  delete:数据库数据删除、缓存数据删除、list后的缓存数据删除

 

各种包的引用和自定义序列化:

  spring-data-redis实现了几种序列化机制 如StringRedisSerializer,GenericJackson2JsonRedisSerializer,JdkSerializationRedisSerializer等等。

  但我们也许需要一种更短,更快,的序列化方式。kryo、msgpack、protobuf都是不错的选择他们各有优势也各有特点。个人感觉protobuf需要一个原有的类才可以被序列化和反序列化本人不是特别会用试了下感觉无法使RedisSerializer保持单例了,而kryo和msgpack却不用而且性能也不错,redis lua 也集成了msgpack。所以这里我选用了msgpack作为自定义的序列化。

以下是我的pom.xml

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>2.8.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-redis</artifactId>
        <version>1.7.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.3</version>
    </dependency>
    <dependency>
        <groupId>com.esotericsoftware.kryo</groupId>
        <artifactId>kryo</artifactId>
        <version>2.24.0</version>
    </dependency>
    <dependency>
        <groupId>org.msgpack</groupId>
        <artifactId>msgpack</artifactId>
        <version>0.6.12</version>
    </dependency>
    <dependency>
        <groupId>com.google.protobuf</groupId>
        <artifactId>protobuf-java</artifactId>
        <version>2.5.0</version>
    </dependency>
</dependencies>

 

这里使用了一个池来创建msgPack

public class MsgPackRedisSerializer<T> implements RedisSerializer<T> {

    private MessagePack msgPack;

    private GenericObjectPool<MessagePack> pool;

    public void setPool(GenericObjectPool<MessagePack> pool) {
        this.pool = pool;
    }

    public byte[] serialize(T t) throws SerializationException {
        if(t==null)
            return new byte[0];
        try {
            msgPack = pool.borrowObject();
        } catch (Exception e1) {
            msgPack = new MessagePack();
        }
        try {
            return msgPack.write(t);
        } catch (IOException e) {
            e.printStackTrace();
            return new byte[0];
        }
    }

    public T deserialize(byte[] bytes) throws SerializationException {
        try {
            msgPack = pool.borrowObject();
        } catch (Exception e1) {
            msgPack = new MessagePack();
        }
        try {
            T t = (T)msgPack.read(bytes);
            pool.returnObject(msgPack);
            return t;
        } catch (IOException e) {
            e.printStackTrace();
            pool.returnObject(msgPack);
            return null;
        }
    }

}

 

posted @ 2016-07-11 10:40  清渡茶叶  阅读(1252)  评论(0编辑  收藏  举报