Java redis redisTemplate

	<!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
package com.cmft.aisp.common.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

import redis.clients.jedis.JedisPoolConfig;


// @Configuration
// @EnableAutoConfiguration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.timeout}")
    private String timeout;

    @Value("${spring.redis.pool.max-idle}")
    private String maxIdle;

    @Value("${spring.redis.pool.min-idle}")
    private String minIdle;

    @Value("${spring.redis.pool.max-active}")
    private String maxTotal;

    @Value("${spring.redis.pool.max-wait}")
    private String maxWaitMillis;

    @Value("${spring.redis.database}")
    private String database;

    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(Integer.parseInt(maxIdle));
        jedisPoolConfig.setMinIdle(Integer.parseInt(minIdle));
        jedisPoolConfig.setMaxTotal(Integer.parseInt(maxTotal));
        jedisPoolConfig.setMaxWaitMillis(Integer.parseInt(maxWaitMillis));
        return jedisPoolConfig;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setPort(Integer.parseInt(port));
        jedisConnectionFactory.setPassword(password);
        jedisConnectionFactory.setTimeout(Integer.parseInt(timeout));
        jedisConnectionFactory.setDatabase(Integer.parseInt(database));
        return jedisConnectionFactory;
    }

    @Bean
    public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);
        redisTemplate.setHashKeySerializer(redisSerializer);
        @SuppressWarnings({ "rawtypes", "unchecked" })
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);//new GenericJackson2JsonRedisSerializer()
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }

    
}
public class RedisTest {

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    /**
     * redis 存放数据利用 redisTemplate
     */
    public void getData() {
        // opsForValue 可以當作map 来用
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();

        //新增一个字符串类型的值,key是键,value是值。
        redisTemplate.opsForValue().set("key","value"); 
        // 获取key键对应的值。
        redisTemplate.opsForValue().get("key");
        // opsForValue.set(key, value, timeout, unit);// timeout 是过期时间 unti 过期时间的单位

        // 在原有的值基础上新增字符串到末尾。
        redisTemplate.opsForValue().append("key", "appendValue");

        // 截取key键对应值得字符串,从开始下标位置开始到结束下标的位置的字符串opsForValue.get(key, start, end)
        opsForValue.get("key", 2, 5);

        // 获取原来key键对应的值并重新赋新值。
        opsForValue.getAndSet("key", "value");

        // 获取指定字符串的长度
        opsForValue.size("key");

        System.out.println("==============================================================================");
        // 判断是否有key所对应的值,有则返回true,没有则返回false
        redisTemplate.hasKey("key");
        // 删除单个key值
        redisTemplate.delete("key");


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值