zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    大数据用户画像方法与实践(干货 转帖)
    前端学习之路
    MySQL学习记录
    Linux 运维之路
    Python学习之路
    NgRx/Store 4 + Angular 5使用教程
    CSS实现各种形状
    CSS3实现背景透明,文字不透明
    CSS实现网页背景图片自适应全屏
    使用Restify+superagent做数据转发
  • 原文地址:https://www.cnblogs.com/zhouheblog/p/15132802.html
Copyright © 2011-2022 走看看