1 Star 0 Fork 0

yajian / spring-data-redis-tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

spring-data-redis-tools

  • RedisTemplate封装工具类 redisTools
  • 可视化分布式ID生成器 distributedId
  • 可靠分布式锁工具类 distributedLock(lua脚本实现原子性解决断电问题、valueId避免错误释放问题)
  • Redis技术总结思维导图

Maven

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

RedisConfiguration

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author wellJay
 */
@Configuration
@EnableCaching
public class RedisConfiguration {
    //过期时间一天
    private static final int DEFAULT_EXPIRE_TIME = 3600 * 24;

    //从配置文件读取redis参数
    @Autowired
    private CloudConfigProperties cloudConfigProperties;

    /**
     * jedisPoolConfig config
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(cloudConfigProperties.getRedis().getMaxIdle());
        jedisPoolConfig.setMinIdle(cloudConfigProperties.getRedis().getMinIdle());
        jedisPoolConfig.setTestOnBorrow(cloudConfigProperties.getRedis().getTestOnBorrow());
        jedisPoolConfig.setTestOnReturn(cloudConfigProperties.getRedis().getTestOnReturn());
        return jedisPoolConfig;
    }

    /**
     * JedisConnectionFactory
     */
    @Bean
    public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(cloudConfigProperties.getRedis().getHost());
        jedisConnectionFactory.setPort(cloudConfigProperties.getRedis().getPort());
        jedisConnectionFactory.setPassword(cloudConfigProperties.getRedis().getPassword());
        jedisConnectionFactory.setTimeout(cloudConfigProperties.getRedis().getTimeout());
        jedisConnectionFactory.setUsePool(true);
        jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
        return jedisConnectionFactory;
    }
    
    /**
     * RedisTemplate
     * 从执行时间上来看,JdkSerializationRedisSerializer是最高效的(毕竟是JDK原生的),但是是序列化的结果字符串是最长的。
     * JSON由于其数据格式的紧凑性,序列化的长度是最小的,时间比前者要多一些。
     * 所以个人的选择是倾向使用JacksonJsonRedisSerializer作为POJO的序列器。 
     */
    @Bean
    public RedisTemplate redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
        RedisTemplate<?, ?> redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        //设置普通value序列化方式
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public CacheManager cacheManager(RedisTemplate redisTemplate) {
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        redisCacheManager.setDefaultExpiration(DEFAULT_EXPIRE_TIME);
        return redisCacheManager;
    }
}

How To Use

1、注入util方式,适用于复杂的业务处理

@Autowired
private RedisCacheUtil redisCacheUtil;

2、Spring注解方式适用于简单的数据缓存

@Cacheable(value = Constants.Redis.SYSTEM, key = ACTIONS_CACHE_KEY)

Redis技术总结

The MIT License (MIT) Copyright (c) 2015 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

spring data redis 封装工具类包含分布式锁(distributedLock)、分布式唯一键(distributedId) 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/ggsss_admin/spring-data-redis-tools.git
git@gitee.com:ggsss_admin/spring-data-redis-tools.git
ggsss_admin
spring-data-redis-tools
spring-data-redis-tools
master

搜索帮助