spring data redis

環境

  • jdk 1.8
  • spring 5.0.12.RELEASE
  • spring-data-redis 2.1.5.RELEASE
  • jedis 2.10.2

要想版本不衝突太艱難了,特此說明版本java

配置jedis

redis.properties

host = 127.0.0.1
port = 6379
timeout = 5000
maxIdle = 100
minIdle = 10
testOnBorrow =true

java config jedis

package com.ssyouxia.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by liyuhang on 2019/2/26.
 */
@Configuration
@PropertySource({"classpath:redis.properties"})
public class JedisConnectionConfiguration {

    @Autowired
    private Environment environment;

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(Integer.parseInt(environment.getProperty("maxIdle")));
        jedisPoolConfig.setMinIdle(Integer.parseInt(environment.getProperty("minIdle")));
        jedisPoolConfig.setTestOnBorrow(Boolean.valueOf(environment.getProperty("testOnBorrow")));
        return jedisPoolConfig;
    }

    @Bean
    public JedisPool jedisPool(JedisPoolConfig jedisPoolConfig) {
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,
                environment.getProperty("host"),
                Integer.parseInt(environment.getProperty("port")),
                Integer.parseInt(environment.getProperty("timeout")));
        return jedisPool;
    }

    @Bean
    public JedisConnectionFactory connectionFactory() {
        final JedisConnectionFactory connectionFactory =
                new JedisConnectionFactory( jedisPoolConfig() );
        return connectionFactory;
    }

}

配置 RedisTemplate

package com.ssyouxia.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Created by lianfangfang on 2019/2/26.
 */
@Configuration
@PropertySource({"classpath:redis.properties"})
@Import(value = JedisConnectionConfiguration.class)
public class SpringDataRedisConfig {



    /*[@Bean](https://my.oschina.net/bean)
    public JedisPoolConfig poolConfig() {
        final JedisPoolConfig jedisPoolConfig =  new JedisPoolConfig();
        jedisPoolConfig.setTestOnBorrow( true );
        jedisPoolConfig.setMaxTotal( 10 );
        return jedisPoolConfig;
    }

    [@Bean](https://my.oschina.net/bean)
    public JedisConnectionFactory connectionFactory() {
        final JedisConnectionFactory connectionFactory =
                new JedisConnectionFactory( poolConfig() );
        connectionFactory.setHostName( "localhost" );
        connectionFactory.setDatabase( Protocol.DEFAULT_DATABASE );
        connectionFactory.setPort( Protocol.DEFAULT_PORT );
        return connectionFactory;
    }*/

    @Bean
    @Autowired
    public RedisTemplate<String, String> redisTemplate(
            final JedisConnectionFactory connectionFactory) {
        final RedisTemplate<String, String> template = new RedisTemplate<String, String>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setStringSerializer(new StringRedisSerializer());
        return template;
    }

}

測試類

package com.ssyouxia;

import com.ssyouxia.config.SpringRootConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Created by lianfangfang on 2019/2/27.
 */
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( classes = SpringRootConfig.class )
public class RedisStringsTestCase {
    @Autowired
    private RedisTemplate< String, String > template;


    @Before
    public void setUp() {
        template.execute( new RedisCallback< Void >() {
            @Override
            public Void doInRedis( RedisConnection connection ) throws DataAccessException {
                connection.flushDb();
                return null;
            }
        } );
    }

    @Test
    public void testSetAndGet() {
        template.opsForValue().set( "mykey", "myvalue" );
        assertThat( template.opsForValue().get( "mykey"), equalTo( "myvalue" ) );
    }


    @Test
    public void testSetAndIncrementAndGet() {
        template.opsForValue().set( "mykey", "10" );
        template.opsForValue().increment( "mykey", 5 );
        assertThat( template.opsForValue().get( "mykey"), equalTo( "15" ) );
    }


    @Test
    public void testPushToListAndGetElementByIndexAndPopFirstElement() {
        template.opsForList().rightPushAll( "mykey", "a", "b", "c", "d" );
        assertThat( template.opsForList().size( "mykey" ), equalTo( 4L ) );
        assertThat( template.opsForList().index( "mykey", 3 ), equalTo( "d" ) );
        assertThat( template.opsForList().leftPop( "mykey" ), equalTo( "a" ) );
        assertThat( template.opsForList().size( "mykey"), equalTo( 3L ) );
    }


    @Test
    public void testAddToSetAndCheckElementExists() {
        template.opsForSet().add( "mykey", "a", "b", "c", "d" );
        assertThat( template.opsForSet().size( "mykey" ), equalTo( 4L ) );
        assertThat( template.opsForSet().isMember( "mykey", "c" ), equalTo( true ) );
        assertThat( template.opsForSet().isMember( "mykey", "e" ), equalTo( false ) );
    }

/*    @Test
    public void testIntersetOperations() {
        template.opsForSet().add( "mykey1", "a", "b", "c", "d" );
        template.opsForSet().add( "mykey2", "c", "d", "e", "f" );

        assertThat( template.opsForSet().intersect( "mykey1", "mykey2" ),
                equalTo( set( "c", "d" ) ) );

        assertThat( template.opsForSet().union( "mykey1", "mykey2" ),
                equalTo( set( "a", "b", "c", "d",  "e", "f" ) ) );

        assertThat( template.opsForSet().difference( "mykey1", "mykey2" ),
                equalTo( set( "a", "b" ) ) );
    }*/


    @Test
    public void testHashOperations() {
        template.opsForHash().put( "mykey", "prop1", "value1"  );
        template.opsForHash().put( "mykey", "prop2", "value2" );

        /*assertThat( template.opsForHash().entries( "mykey" ),
                equalTo( map( "prop1", "value1", "prop2", "value2" ) ) );*/

        assertThat( template.opsForHash().get( "mykey", "prop1" ),
                equalTo( ( Object )"value1" ) );

        template.opsForHash().delete( "mykey", "prop1", "prop2" );
        assertThat( template.opsForHash().get( "mykey", "prop1" ),
                equalTo( null ) );
    }


    @Test
    public void testTransaction() {
        template.opsForValue().set( "mykey1", "10" );

        template.opsForSet().add( "mykey2", "a" );
        assertThat( template.opsForSet().isMember( "mykey2", "b"), equalTo( false ) );

        template.execute( new SessionCallback< List< Object > >() {
            @SuppressWarnings("unchecked")
            @Override
            public< K, V > List<Object> execute(final RedisOperations< K, V > operations )
                    throws DataAccessException {

                operations.watch( ( K )"mykey1" );
                operations.multi();

                operations.opsForValue().increment( ( K )"mykey1", 5 );
                operations.opsForSet().add( ( K )"mykey2", ( V )"b" );

                return operations.exec();
            }
        } );

        assertThat( template.opsForValue().get( "mykey1"), equalTo( "15" ) );
        assertThat( template.opsForSet().isMember( "mykey2", "b"), equalTo( true ) );
    }


    @Test
    public void testPipelining() {
        template.opsForValue().set( "mykey1", "10" );

        template.executePipelined( new RedisCallback< Object >() {
            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                for( int i = 0; i < 100; ++i ) {
                    template.opsForValue().increment( "mykey1", 1 );
                }
                return null;
            }
        } );

        assertThat( template.opsForValue().get( "mykey1"), equalTo( "110" ) );
    }

}

項目下載地址

https://gitee.com/liyuhang712/wsocketdemogit

相關文章
相關標籤/搜索