spring data redis pipeline

9491 ワード

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 *      
 *  @author chenjianfei
 */
@Configuration
public class RedisConfiguration {

    @Bean
    public JedisClientConfiguration getJedisClientConfiguration() {
        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder JedisPoolingClientConfigurationBuilder = (
                JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();
        GenericObjectPoolConfig GenericObjectPoolConfig = new GenericObjectPoolConfig();
        GenericObjectPoolConfig.setMaxIdle(1000);
        GenericObjectPoolConfig.setMaxTotal(100);
        GenericObjectPoolConfig.setMinIdle(100);
        return JedisPoolingClientConfigurationBuilder.poolConfig(GenericObjectPoolConfig).build();
    }

    @Bean
    public JedisConnectionFactory getJedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(0);
        redisStandaloneConfiguration.setHostName("localhost");
        redisStandaloneConfiguration.setPassword(RedisPassword.none());
        redisStandaloneConfiguration.setPort(6379);
        return new JedisConnectionFactory(redisStandaloneConfiguration, getJedisClientConfiguration());
    }

    @Bean(value = "stringRedisTemplate")
    public RedisTemplate getStringRedisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(getJedisConnectionFactory());
        return redisTemplate;
    }

    @Bean(value = "objectRedisTemplate")
    public RedisTemplate getObjectRedisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setConnectionFactory(getJedisConnectionFactory());
        return redisTemplate;
    }
}
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author chenjianfei
 */
@Service
public class TestService {
    @Resource(name = "stringRedisTemplate")
    private RedisTemplate stringRedisTemplate;
    @Resource(name = "objectRedisTemplate")
    private RedisTemplate objectRedisTemplate;

    /**
     *   String   
     *
     * @param key
     * @param value
     */
    public void setStringValue(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    /**
     *   String   
     *
     * @param key
     * @return
     */
    public String getStringValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     *   Object   
     *
     * @param key
     * @param value
     */
    public void setObjectValue(String key, Serializable value) {
        objectRedisTemplate.opsForValue().set(key, value);
    }

    /**
     *   Object   
     *
     * @param key
     * @return
     */
    public Object getObjectValue(String key) {
        return objectRedisTemplate.opsForValue().get(key);
    }

    /**
     *   Bound   
     *
     * @param key
     * @return
     */
    public void setBoundObjectValue(String key) {
        //  key     key  ,             key
        BoundValueOperations boundValueOperations = objectRedisTemplate.boundValueOps(key);
        boundValueOperations.set(new Demo2());
        //   
        boundValueOperations.set(new Demo3());
        boundValueOperations.expire(60, TimeUnit.MINUTES);
    }

    /**
     *   Bound   
     *
     * @param key
     * @return
     */
    public Object getBoundObjectValue(String key) {
        BoundValueOperations boundValueOperations = objectRedisTemplate.boundValueOps(key);
        return boundValueOperations.get();
    }

    /**
     * pipeline : 1,      
     */
    public void pipelineSample(String key) {
        StringRedisSerializer stringRedisSerializer = (StringRedisSerializer) stringRedisTemplate.getKeySerializer();
        //  key   byte[]
        final byte[] rawKey = stringRedisSerializer.serialize(key);
        //pipeline
        RedisCallback> pipelineCallback = new RedisCallback>() {
            @Override
            public List doInRedis(RedisConnection connection) throws DataAccessException {
                connection.openPipeline();
                //pipeline    
                connection.incr(rawKey);
                connection.incr(rawKey);
                return connection.closePipeline();
            }

        };

        List results = (List) objectRedisTemplate.execute(pipelineCallback);
        for (Object item : results) {
            System.out.println(item.toString());
        }
    }

    //pipeline:    
    public void pipelineSample2(String key) {
        StringRedisSerializer stringRedisSerializer = (StringRedisSerializer) stringRedisTemplate.getKeySerializer();
        //  key   byte[]
        final byte[] rawKey = stringRedisSerializer.serialize(key);
        RedisConnectionFactory factory = objectRedisTemplate.getConnectionFactory();
        RedisConnection redisConnection = factory.getConnection();
        List results;
        try {
            redisConnection.openPipeline();
            redisConnection.incr(rawKey);
            results = redisConnection.closePipeline();
        } finally {
            RedisConnectionUtils.releaseConnection(redisConnection, factory);
        }
        if (results == null) {
            return;
        }
        for (Object item : results) {
            System.out.println(item.toString());
        }
    }

}

class Demo2 implements Serializable {

}

class Demo3 implements Serializable {

}
import org.springframework.context.annotation.*;

@ComponentScan
public class Application {

}
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import vip.fkandy.redis.Application;

import java.io.Serializable;

public class TestServiceTest{
    TestService testService = null;
    @Before
    public void setUp() throws Exception {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(Application.class);
        testService = context.getBean(TestService.class);
    }
    @Test
    public void setStringValue() {
        testService.setStringValue("k1","v1");
    }

    @Test
    public void getStringValue() {
        System.out.println(testService.getStringValue("k1"));
    }

    @Test
    public void setObjectValue() {
        testService.setObjectValue("k2",new Demo());
    }

    @Test
    public void getObjectValue() {
        System.out.println(testService.getObjectValue("k2"));
    }

    @Test
    public void setBoundValue() {
        testService.setBoundObjectValue("k4");
    }

    @Test
    public void pipelineSample() {
        testService.pipelineSample("k4");
    }
    @Test
    public void pipelineSample2() {
        testService.pipelineSample2("k4");
    }
}
class Demo implements Serializable{

}