spring RedisTemplate用法

1.maven依赖

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.6.2.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.2</version>
        </dependency>

2.spring的xml配置

    <bean id="jedisFactory" class="work.cache.redis.JedisPoolConnectionFactory"
        destroy-method="close">
        <constructor-arg ref="jedisPoolConfig" />
        <constructor-arg value="127.0.0.1" />
        <constructor-arg value="6379" />
    </bean>

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="127.0.0.1" />
        <property name="port" value="6379"/>
        <property name="poolConfig" ref="jedisPoolConfig" />
        <property name="usePool" value="true"/>
    </bean>

    <bean id="springRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>
    </bean>

2.2 将bean传到业务类中

 

 3 使用redisTemplate

 

 springRedisTemplate.opsForValue().set(String.valueOf(report.getId()) +InstructionUtil.IsuSuffixRedisKey.ISU_HEART_BEAT.getKey(),1,80,TimeUnit.SECONDS);

RedisTemplate依赖:import org.springframework.data.redis.core.RedisTemplate;

 参考地址:https://blog.csdn.net/liang_love_java/article/details/50497281

posted @ 2021-08-12 15:05  农名工进城  阅读(458)  评论(0编辑  收藏  举报