Spring use Redis

1. dependence introduction

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.1.9.RELEASE</version>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.1.0</version>
</dependency>

2. Configure (applicationContext.xml)

1. Spring configuration JedisPoolConfig objects (connection pool)

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <!--最大空闲数-->
    <property name="maxIdle" value="50"/>
    <!--最大连接数-->
    <property name="maxTotal" value="100"/>
    <!--最大等待时间-->
    <property name="maxWaitMillis" value="20000"/>
</bean>

2. The plant model to the connection pool configuration

<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <!--Redis服务地址-->
    <property name="hostName" value="localhost"/>
    <!--端口号-->
    <property name="port" value="6379"/>
    <!--如果有密码则需要配置密码-->
    <!--<property name="password" value="password"/>-->
    <!--连接池配置-->
    <property name="poolConfig" ref="poolConfig"/>
</bean>

3. Configure RedisTemplate

<bean id="redisTemplate"
      class="org.springframework.data.redis.core.RedisTemplate"
      p:connection-factory-ref="connectionFactory"/>
</bean>

Note: there is no way an ordinary connection directly to Redis objects directly into memory, we need an alternative solution: the object serialization (can be simply understood as inherited Serializable interface). Redis is stored in the cache after we can object serialization, then removed when they pass through the converter object after serialization deserialization back to the object, all need to POJO objects implement Serializable

3.RedisTemplate basic use

StringRedisTemplate and RedisTemplate
relationship between the two is StringRedisTemplate inherited RedisTemplate.
Both the data are not in common:
StringRedisTemplate StringRedisTemplate only inside the management data, RedisTemplate only the management data RedisTemplate.
SDR sequence strategy used by default, there are two, one is String serialization strategy, one is JDK serialization strategy.
StringRedisTemplate defaults is String serialization strategy, save the key and value are based on the sequence of this policy saved.
RedisTemplate uses a default JDK serialization strategy, save the key and value are based on the sequence of this policy saved.

1. redisTemplate.opsForValue().set("student_1", student);
After 2.10 seconds Failure
void set(K key, V value, long timeout, TimeUnit unit);
redisTemplate.opsForValue().set("name","tom",10, TimeUnit.SECONDS);
3. string value with the given key value stored override parameters (Overwrite), starting at offset offset
void set(K key, V value, long offset);
4. absence was provided, disposed Returns true success, failure to return to false
Boolean setIfAbsent(K key, V value);
5. The of a plurality of keys set their values
void multiSet(Map<? extends K, ? extends V> m);
to the plurality of keys and their values are taken
List<V> multiGet(Collection<K> keys);

        Map<String,String> maps = new HashMap<String, String>();
        maps.put("multi1","multi1");
        maps.put("multi2","multi2");
        maps.put("multi3","multi3");
        template.opsForValue().multiSet(maps);

        List<String> keys = new ArrayList<String>();
        keys.add("multi1");
        keys.add("multi2");
        keys.add("multi3");
        template.opsForValue().multiGet(keys);

6. A plurality of keys are provided for the value thereof, if present, returns false, returns true absence
Boolean multiSetIfAbsent(Map<? extends K, ? extends V> m);
7. Set key string value and returns its old value
T getAndSet(K key, T value); 8.如果key已经存在并且是一个字符串,则该命令将该值追加到字符串的末尾。 如果键不存在,则它被创建并设置为空字符串并追加,因此APPEND在这种特殊情况下将类似于SET。Integer the append (K Key, String value); 9.截取key所对应的value字符串String GET (Key K , Long Start, End Long); 10.返回key所对应的value值得长度Long size (K Key); `

Guess you like

Origin www.cnblogs.com/loveer/p/11316279.html