【Redis】Redis在项目中的配置

目录

SpringBoot集成Redis:

Spring集成redis:


SpringBoot集成Redis:

pom文件中引入依赖


		
			org.springframework.boot
			spring-boot-starter-data-redis
		

配置yml文件:

【Redis】Redis在项目中的配置_第1张图片

 

添加RedisConfig配置类:



/**
 * 实例:
 * 1.注解使用:
 *
 * @Cacheable(value = "configDetail", key = "#p0", sync = true)
 * @CacheExpire(expire = 120)
 * ConfigDetailEntity findByAction(String action);
 * 

* 2.直接使用 * redisTemplate.opsForValue();//操作字符串 * redisTemplate.opsForHash();//操作hash * redisTemplate.opsForList();//操作list * redisTemplate.opsForSet();//操作set * redisTemplate.opsForZSet();//操作有序set */ @Configuration @Slf4j public class RedisConfig extends CachingConfigurerSupport { private final RedisConnectionFactory redisConnectionFactory; private RedisSerializer keySerializer = new StringRedisSerializer(); private final RedisSerializationContext.SerializationPair keyPair = RedisSerializationContext .SerializationPair.fromSerializer(keySerializer); private RedisSerializer valueSerializer = new GenericFastJsonRedisSerializer(); private final RedisSerializationContext.SerializationPair valuePair = RedisSerializationContext .SerializationPair.fromSerializer(valueSerializer); RedisConfig(RedisConnectionFactory redisConnectionFactory) { this.redisConnectionFactory = redisConnectionFactory; } @Bean public RedisTemplate redisTemplate() { RedisTemplate template = new RedisTemplate<>(); // 设置key序列化类,否则key前面会多了一些乱码 template.setKeySerializer(keySerializer); template.setHashKeySerializer(keySerializer); // fastjson serializer template.setValueSerializer(valueSerializer); template.setHashValueSerializer(valueSerializer); // 如果 KeySerializer 或者 ValueSerializer 没有配置,则对应的 KeySerializer、ValueSerializer 才使用这个 Serializer template.setDefaultSerializer(valueSerializer); log.info("redis: {}", redisConnectionFactory); LettuceConnectionFactory factory = (LettuceConnectionFactory) redisConnectionFactory; log.info("spring.redis.database: {}", factory.getDatabase()); log.info("spring.redis.host: {}", factory.getHostName()); log.info("spring.redis.port: {}", factory.getPort()); log.info("spring.redis.timeout: {}", factory.getTimeout()); log.info("spring.redis.password: {}", factory.getPassword()); // factory template.setConnectionFactory(redisConnectionFactory); template.afterPropertiesSet(); return template; } @Bean @Override public KeyGenerator keyGenerator() { return (o, method, objects) -> { StringBuilder sb = new StringBuilder(32); sb.append(o.getClass().getSimpleName()); sb.append("."); sb.append(method.getName()); if (objects.length > 0) { sb.append("#"); } String sp = ""; for (Object object : objects) { sb.append(sp); if (object == null) { sb.append("NULL"); } else { sb.append(object.toString()); } sp = "."; } return sb.toString(); }; } /** * 配置 RedisCacheManager,使用 cache 注解管理 redis 缓存 */ @Bean @Override public CacheManager cacheManager() { TedisCacheManager tedisCacheManager = TedisCacheManager.create(redisConnectionFactory); tedisCacheManager.setKeySerializer(keySerializer); tedisCacheManager.setValueSerializer(valueSerializer); return tedisCacheManager; } }

 

RedisTemplate的使用:

bean注入:

@Resource
    public RedisTemplate redisTemplate;
redisTemplate.opsForValue();//操作字符串

redisTemplate.opsForHash();//操作hash

redisTemplate.opsForList();//操作list

redisTemplate.opsForSet();//操作set

redisTemplate.opsForZSet();//操作有序set
String类型的操作:

redisTemplate.opsForValue().set(keyText, internResume);
redisTemplate.expire(keyText, 60, TimeUnit.SECONDS); // 设置过期时间
String i = (String) redisTemplate.opsForValue().get(keyText);

详细操作可以看:https://www.cnblogs.com/songanwei/p/9274348.html

Spring集成redis:

pom中引入的依赖:

       
        
            org.springframework.data
            spring-data-redis
            ${spring.data.redis.version}
        

        
        
            redis.clients
            jedis
            ${redis.clients.version}
        

 

你可能感兴趣的:(Redis)