Commit 3556e454 authored by 张辰's avatar 张辰

redis cache

parent c3c96356
...@@ -53,6 +53,17 @@ ...@@ -53,6 +53,17 @@
<!-- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>--> <!-- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>-->
<!-- </dependency>--> <!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
......
...@@ -2,11 +2,13 @@ package com.tanpu.community; ...@@ -2,11 +2,13 @@ package com.tanpu.community;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication @SpringBootApplication
@EnableTransactionManagement @EnableTransactionManagement
@EnableCaching
@ComponentScan({"com.tanpu.common", "com.tanpu.community"}) @ComponentScan({"com.tanpu.common", "com.tanpu.community"})
public class CommunityApplication { public class CommunityApplication {
......
package com.tanpu.community.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
import java.time.Duration;
@Configuration
@EnableCaching
public class CacheConfig extends CachingConfigurerSupport {
// @Value("${spring.redis.host}")
// private String host;
// @Value("${spring.redis.port}")
// private int port;
// @Value("${spring.redis.password}")
// private String password;
// @Value("${spring.redis.timeout}")
// private int timeout;
// see https://www.baeldung.com/spring-boot-redis-cache
@Bean
public RedisCacheConfiguration cacheConfiguration() {
return RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofDays(30))
.disableCachingNullValues()
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()));
}
@Bean
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
return (builder) -> builder
.withCacheConfiguration("tempCache",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(60)))
.withCacheConfiguration("longCache",
RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(365)));
}
@Bean("communityKeyGenerator")
public KeyGenerator keyGenerator() {
return new CommunityKeyGenerator();
}
public static class CommunityKeyGenerator implements KeyGenerator {
public Object generate(Object target, Method method, Object... params) {
return "new_community_" + target.getClass().getSimpleName() + "_"
+ method.getName() + "_"
+ StringUtils.arrayToDelimitedString(params, "_");
}
}
}
package com.tanpu.community.controller; package com.tanpu.community.controller;
import com.alibaba.fastjson.JSON;
import com.tanpu.community.dao.entity.community.FansRelEntity; import com.tanpu.community.dao.entity.community.FansRelEntity;
import com.tanpu.community.manager.HomePageManager; import com.tanpu.community.manager.HomePageManager;
import com.tanpu.community.model.req.homepage.AddIdolReq; import com.tanpu.community.model.req.homepage.AddIdolReq;
...@@ -24,6 +25,7 @@ public class HomePageController { ...@@ -24,6 +25,7 @@ public class HomePageController {
public String queryMyFans() { public String queryMyFans() {
String userId = "123"; String userId = "123";
List<FansRelEntity> fans = homePageManager.queryFansByIdolId(userId); List<FansRelEntity> fans = homePageManager.queryFansByIdolId(userId);
System.out.println(fans.size());
return null; return null;
} }
......
...@@ -3,11 +3,14 @@ package com.tanpu.community.service; ...@@ -3,11 +3,14 @@ package com.tanpu.community.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.community.dao.entity.community.FansRelEntity; import com.tanpu.community.dao.entity.community.FansRelEntity;
import com.tanpu.community.dao.mapper.community.FansRelMapper; import com.tanpu.community.dao.mapper.community.FansRelMapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
@EnableCaching
@Service @Service
public class FansRelService { public class FansRelService {
...@@ -18,12 +21,12 @@ public class FansRelService { ...@@ -18,12 +21,12 @@ public class FansRelService {
return fansRelMapper.selectList(new LambdaQueryWrapper<FansRelEntity>().eq(FansRelEntity::getFollowerId, followerId)); return fansRelMapper.selectList(new LambdaQueryWrapper<FansRelEntity>().eq(FansRelEntity::getFollowerId, followerId));
} }
@Cacheable(value = "tempCache", keyGenerator = "communityKeyGenerator")
public List<FansRelEntity> queryFansByIdolId(String idolId) { public List<FansRelEntity> queryFansByIdolId(String idolId) {
return fansRelMapper.selectList(new LambdaQueryWrapper<FansRelEntity>().eq(FansRelEntity::getIdolId, idolId)); return fansRelMapper.selectList(new LambdaQueryWrapper<FansRelEntity>().eq(FansRelEntity::getIdolId, idolId));
} }
public void addFans(String idolId, String followerId) { public void addFans(String idolId, String followerId) {
FansRelEntity rel = new FansRelEntity(); FansRelEntity rel = new FansRelEntity();
rel.setIdolId(idolId); rel.setIdolId(idolId);
rel.setFollowerId(followerId); rel.setFollowerId(followerId);
......
...@@ -23,6 +23,18 @@ spring.datasource: ...@@ -23,6 +23,18 @@ spring.datasource:
minIdle: 2 minIdle: 2
initialSize: 2 initialSize: 2
spring.redis:
host: 118.190.63.109
port: 56379
password: qimeng123
timeout: 2000
jedis:
pool:
max-active: 3
max-idle: 3
min-idle: 3
spring: spring:
sleuth: sleuth:
enabled: false enabled: false
......
...@@ -55,6 +55,13 @@ ...@@ -55,6 +55,13 @@
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.4.5</version>
</dependency>
<dependency> <dependency>
<groupId>com.baomidou</groupId> <groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId> <artifactId>mybatis-plus-boot-starter</artifactId>
...@@ -67,6 +74,11 @@ ...@@ -67,6 +74,11 @@
<version>3.4.1</version> <version>3.4.1</version>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency> <dependency>
<groupId>com.github.pagehelper</groupId> <groupId>com.github.pagehelper</groupId>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment