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

  • 相关阅读:
    C#获取MAC地址
    C#洗牌
    删除前提示
    简单MD5加密
    读取EXCEL所有列名C#
    gridview二次加载样式丢失
    迅雷API接口(及示例演示)
    [转]Servlet过滤器介绍之原理分析
    [转]MySQL运行状态show status中文详解
    [转]如何修改mysql root密码
  • 原文地址:https://www.cnblogs.com/zhouheblog/p/15132802.html
Copyright © 2011-2022 走看看