Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
7132ad8e
Commit
7132ad8e
authored
Jul 15, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis cache
parent
acb44665
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
188 additions
and
132 deletions
+188
-132
FormerThemeQo.java
.../java/com/tanpu/community/api/beans/qo/FormerThemeQo.java
+4
-1
ThemeContentQo.java
...java/com/tanpu/community/api/beans/qo/ThemeContentQo.java
+4
-1
ThemeQo.java
...c/main/java/com/tanpu/community/api/beans/qo/ThemeQo.java
+3
-3
pom.xml
community-service/pom.xml
+5
-0
CacheEvict.java
...e/src/main/java/com/tanpu/community/cache/CacheEvict.java
+12
-0
CacheGet.java
...ice/src/main/java/com/tanpu/community/cache/CacheGet.java
+13
-0
HomePageCache.java
...rc/main/java/com/tanpu/community/cache/HomePageCache.java
+0
-22
RedisCacheEvictAspect.java
...java/com/tanpu/community/cache/RedisCacheEvictAspect.java
+41
-0
RedisCacheGetAspect.java
...n/java/com/tanpu/community/cache/RedisCacheGetAspect.java
+58
-0
RedisConfig.java
...src/main/java/com/tanpu/community/config/RedisConfig.java
+0
-88
RestTemplateConfig.java
...n/java/com/tanpu/community/config/RestTemplateConfig.java
+0
-16
ThemeManager.java
...c/main/java/com/tanpu/community/manager/ThemeManager.java
+2
-1
SpringUtils.java
...e/src/main/java/com/tanpu/community/util/SpringUtils.java
+40
-0
pom.xml
pom.xml
+6
-0
No files found.
community-api/src/main/java/com/tanpu/community/api/beans/qo/FormerThemeQo.java
View file @
7132ad8e
...
@@ -7,6 +7,7 @@ import lombok.Builder;
...
@@ -7,6 +7,7 @@ import lombok.Builder;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.NoArgsConstructor
;
import
java.io.Serializable
;
import
java.util.List
;
import
java.util.List
;
@Data
@Data
...
@@ -14,7 +15,9 @@ import java.util.List;
...
@@ -14,7 +15,9 @@ import java.util.List;
@AllArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor
@ApiModel
(
value
=
"转发的主题"
)
@ApiModel
(
value
=
"转发的主题"
)
public
class
FormerThemeQo
{
public
class
FormerThemeQo
implements
Serializable
{
private
static
final
long
serialVersionUID
=
837798765255929228L
;
@ApiModelProperty
(
value
=
"被转发的昵称"
)
@ApiModelProperty
(
value
=
"被转发的昵称"
)
private
String
nickName
;
private
String
nickName
;
...
...
community-api/src/main/java/com/tanpu/community/api/beans/qo/ThemeContentQo.java
View file @
7132ad8e
...
@@ -5,11 +5,14 @@ import io.swagger.annotations.ApiModel;
...
@@ -5,11 +5,14 @@ import io.swagger.annotations.ApiModel;
import
io.swagger.annotations.ApiModelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
lombok.Data
;
import
java.io.Serializable
;
import
java.util.List
;
import
java.util.List
;
@Data
@Data
@ApiModel
(
value
=
"主题内容"
)
@ApiModel
(
value
=
"主题内容"
)
public
class
ThemeContentQo
{
public
class
ThemeContentQo
implements
Serializable
{
private
static
final
long
serialVersionUID
=
100042751824238477L
;
@ApiModelProperty
(
value
=
"RelTypeEnum类型,8:文本,88:产品 3:直播 6:短视频 2:课程,9:图片 10:多图(讨论)"
)
@ApiModelProperty
(
value
=
"RelTypeEnum类型,8:文本,88:产品 3:直播 6:短视频 2:课程,9:图片 10:多图(讨论)"
)
private
String
type
;
private
String
type
;
...
...
community-api/src/main/java/com/tanpu/community/api/beans/qo/ThemeQo.java
View file @
7132ad8e
...
@@ -5,14 +5,14 @@ import io.swagger.annotations.ApiModel;
...
@@ -5,14 +5,14 @@ import io.swagger.annotations.ApiModel;
import
io.swagger.annotations.ApiModelProperty
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
lombok.Data
;
import
java.io.Serializable
;
import
java.util.List
;
import
java.util.List
;
@Data
@Data
@ApiModel
(
"主题信息流对象"
)
@ApiModel
(
"主题信息流对象"
)
public
class
ThemeQo
{
public
class
ThemeQo
implements
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
private
static
final
long
serialVersionUID
=
-
8585648478551597699L
;
@ApiModelProperty
(
value
=
"主题ID"
)
@ApiModelProperty
(
value
=
"主题ID"
)
private
String
themeId
;
private
String
themeId
;
...
...
community-service/pom.xml
View file @
7132ad8e
...
@@ -33,6 +33,11 @@
...
@@ -33,6 +33,11 @@
<artifactId>
velocity-engine-core
</artifactId>
<artifactId>
velocity-engine-core
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-aop
</artifactId>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
...
...
community-service/src/main/java/com/tanpu/community/cache/CacheEvict.java
0 → 100644
View file @
7132ad8e
package
com
.
tanpu
.
community
.
cache
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
(
ElementType
.
METHOD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
CacheEvict
{
String
prefix
()
default
""
;
}
community-service/src/main/java/com/tanpu/community/cache/CacheGet.java
0 → 100644
View file @
7132ad8e
package
com
.
tanpu
.
community
.
cache
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
@Target
(
ElementType
.
METHOD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
CacheGet
{
String
prefix
()
default
""
;
int
expireSeconds
()
default
60
;
}
community-service/src/main/java/com/tanpu/community/cache/HomePageCache.java
deleted
100644 → 0
View file @
acb44665
package
com
.
tanpu
.
community
.
cache
;
import
com.tanpu.community.dao.entity.community.HomePageEntity
;
import
com.tanpu.community.service.HomePageService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
java.util.Map
;
/**
* 用户信息缓存类
*/
public
class
HomePageCache
{
private
Map
<
String
,
HomePageEntity
>
homePageCache
;
@Autowired
private
HomePageService
homePageService
;
}
community-service/src/main/java/com/tanpu/community/cache/RedisCacheEvictAspect.java
0 → 100644
View file @
7132ad8e
package
com
.
tanpu
.
community
.
cache
;
import
com.alibaba.fastjson.JSON
;
import
com.tanpu.common.redis.RedisHelper
;
import
com.tanpu.common.redis.RedisKeyHelper
;
import
com.tanpu.community.util.SpringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.springframework.stereotype.Component
;
import
java.time.Duration
;
@Aspect
@Component
public
class
RedisCacheEvictAspect
{
private
static
RedisKeyHelper
keyHelper
=
new
RedisKeyHelper
(
"community2_cache:"
);
@Around
(
value
=
"@annotation(com.tanpu.community.cache.CacheEvict)"
)
public
Object
methodAround
(
ProceedingJoinPoint
jp
)
throws
Throwable
{
MethodSignature
ms
=
(
MethodSignature
)
jp
.
getSignature
();
CacheEvict
anno
=
ms
.
getMethod
().
getAnnotation
(
CacheEvict
.
class
);
String
prefix
=
anno
.
prefix
();
RedisHelper
redisHelper
=
SpringUtils
.
getBean
(
RedisHelper
.
class
);
String
key
=
keyHelper
.
buildKeyCustom
(
":"
,
prefix
,
StringUtils
.
joinWith
(
":"
,
jp
.
getArgs
()));
redisHelper
.
delete
(
key
);
try
{
Object
ret
=
jp
.
proceed
();
return
ret
;
}
catch
(
Throwable
t
)
{
// todo wrap this throwable
throw
new
RuntimeException
(
t
);
}
}
}
community-service/src/main/java/com/tanpu/community/cache/RedisCacheGetAspect.java
0 → 100644
View file @
7132ad8e
package
com
.
tanpu
.
community
.
cache
;
import
com.alibaba.fastjson.JSON
;
import
com.tanpu.common.redis.RedisHelper
;
import
com.tanpu.common.redis.RedisKeyHelper
;
import
com.tanpu.community.util.SpringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.springframework.stereotype.Component
;
import
java.time.Duration
;
@Aspect
@Component
public
class
RedisCacheGetAspect
{
private
static
RedisKeyHelper
keyHelper
=
new
RedisKeyHelper
(
"community2_cache:"
);
@Around
(
value
=
"@annotation(com.tanpu.community.cache.CacheGet)"
)
public
Object
methodAround
(
ProceedingJoinPoint
jp
)
throws
Throwable
{
MethodSignature
ms
=
(
MethodSignature
)
jp
.
getSignature
();
CacheGet
anno
=
ms
.
getMethod
().
getAnnotation
(
CacheGet
.
class
);
String
prefix
=
anno
.
prefix
();
int
expireSeconds
=
anno
.
expireSeconds
();
if
(
expireSeconds
<
0
)
{
throw
new
RuntimeException
(
"expire second cannot be negative."
);
}
RedisHelper
redisHelper
=
SpringUtils
.
getBean
(
RedisHelper
.
class
);
String
key
=
keyHelper
.
buildKeyCustom
(
":"
,
prefix
,
StringUtils
.
joinWith
(
":"
,
jp
.
getArgs
()));
String
value
=
redisHelper
.
get
(
key
);
// todo 考虑缓存穿透的问题.
if
(
StringUtils
.
isNotBlank
(
value
))
{
return
JSON
.
parseObject
(
value
,
ms
.
getReturnType
());
}
try
{
Object
ret
=
jp
.
proceed
();
String
putValue
=
ret
==
null
?
"null"
:
JSON
.
toJSONString
(
ret
);
if
(
expireSeconds
==
0
)
{
redisHelper
.
set
(
key
,
putValue
);
}
else
{
redisHelper
.
set
(
key
,
putValue
,
Duration
.
ofSeconds
(
expireSeconds
));
}
return
ret
;
}
catch
(
Throwable
t
)
{
// todo wrap this throwable
throw
new
RuntimeException
(
t
);
}
}
}
community-service/src/main/java/com/tanpu/community/config/RedisConfig.java
deleted
100644 → 0
View file @
acb44665
package
com
.
tanpu
.
community
.
config
;
import
org.springframework.beans.factory.annotation.Value
;
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.util.StringUtils
;
import
java.lang.reflect.Method
;
@Configuration
@EnableCaching
public
class
RedisConfig
{
@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
;
@Value
(
"${spring.redis.max-active}"
)
private
int
maxActive
;
@Value
(
"${spring.redis.max-wait}"
)
private
long
maxWaitMillis
;
@Value
(
"${spring.redis.max-idle}"
)
private
int
maxIdle
;
// @Bean
// public JedisPoolConfig jedisPoolConfig() {
// JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// // maximum connection
// jedisPoolConfig.setMaxTotal(maxActive);
// // Maximum wait time when no connection is available in the pool
// jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
// // Maximum number of idle connections
// jedisPoolConfig.setMinIdle(maxIdle);
// // Other properties can be added by yourself
// return jedisPoolConfig;
// }
//
// @Bean
// public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
// JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder().usePooling()
// .poolConfig(jedisPoolConfig).and().readTimeout(Duration.ofMillis(timeout)).build();
// RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
// redisStandaloneConfiguration.setHostName(host);
// redisStandaloneConfiguration.setPort(port);
// redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
// return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
// }
//
// @Bean
// public RedisTemplate<String, Object> redisTemplate() {
// RedisTemplate<String, Object> template = new RedisTemplate<>();
// template.setConnectionFactory(jedisConnectionFactory(jedisPoolConfig()));
//
// return template;
// }
//
// /**
// * cache
// */
// @Bean
// public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() {
// return (builder) -> RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(jedisConnectionFactory(jedisPoolConfig()))
// .withCacheConfiguration("tempCache",
// RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(10)))
// .withCacheConfiguration("longCache",
// RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofDays(7)));
// }
@Bean
(
"communityKeyGenerator"
)
public
KeyGenerator
keyGenerator
()
{
return
new
RedisConfig
.
CommunityKeyGenerator
();
}
public
static
class
CommunityKeyGenerator
implements
KeyGenerator
{
public
Object
generate
(
Object
target
,
Method
method
,
Object
...
params
)
{
// todo prefix 加到common
return
"new_community_"
+
target
.
getClass
().
getSimpleName
()
+
"_"
+
method
.
getName
()
+
"_"
+
StringUtils
.
arrayToDelimitedString
(
params
,
"_"
);
}
}
}
community-service/src/main/java/com/tanpu/community/config/RestTemplateConfig.java
deleted
100644 → 0
View file @
acb44665
package
com
.
tanpu
.
community
.
config
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.web.client.RestTemplate
;
/**
* created by xd on 2019/9/29
*/
@Configuration
public
class
RestTemplateConfig
{
@Bean
public
RestTemplate
getRestTemplate
()
{
return
new
RestTemplate
();
}
}
community-service/src/main/java/com/tanpu/community/manager/ThemeManager.java
View file @
7132ad8e
...
@@ -10,6 +10,7 @@ import com.tanpu.community.api.enums.BlockTypeEnum;
...
@@ -10,6 +10,7 @@ import com.tanpu.community.api.enums.BlockTypeEnum;
import
com.tanpu.community.api.enums.CollectionTypeEnum
;
import
com.tanpu.community.api.enums.CollectionTypeEnum
;
import
com.tanpu.community.api.enums.ThemeListTypeEnum
;
import
com.tanpu.community.api.enums.ThemeListTypeEnum
;
import
com.tanpu.community.api.enums.ThemeTypeEnum
;
import
com.tanpu.community.api.enums.ThemeTypeEnum
;
import
com.tanpu.community.cache.CacheGet
;
import
com.tanpu.community.dao.entity.community.*
;
import
com.tanpu.community.dao.entity.community.*
;
import
com.tanpu.community.service.*
;
import
com.tanpu.community.service.*
;
import
com.tanpu.community.service.other.BlackListService
;
import
com.tanpu.community.service.other.BlackListService
;
...
@@ -133,8 +134,8 @@ public class ThemeManager {
...
@@ -133,8 +134,8 @@ public class ThemeManager {
//查询正文
//查询正文
@CacheGet
(
prefix
=
"getThemeDetail"
,
expireSeconds
=
600
)
public
ThemeQo
getDetail
(
String
themeId
,
String
userId
)
{
public
ThemeQo
getDetail
(
String
themeId
,
String
userId
)
{
ThemeEntity
themeEntity
=
themeService
.
queryByThemeId
(
themeId
);
ThemeEntity
themeEntity
=
themeService
.
queryByThemeId
(
themeId
);
if
(
themeEntity
==
null
)
{
if
(
themeEntity
==
null
)
{
throw
new
BizException
(
"找不到帖子id:"
+
themeId
);
throw
new
BizException
(
"找不到帖子id:"
+
themeId
);
...
...
community-service/src/main/java/com/tanpu/community/util/SpringUtils.java
0 → 100644
View file @
7132ad8e
package
com
.
tanpu
.
community
.
util
;
import
org.springframework.beans.BeansException
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContextAware
;
import
org.springframework.stereotype.Component
;
@Component
public
class
SpringUtils
implements
ApplicationContextAware
{
private
static
ApplicationContext
applicationContext
;
@Override
public
void
setApplicationContext
(
ApplicationContext
applicationContext
)
throws
BeansException
{
if
(
SpringUtils
.
applicationContext
==
null
)
{
SpringUtils
.
applicationContext
=
applicationContext
;
}
}
//获取applicationContext
public
static
ApplicationContext
getApplicationContext
()
{
return
applicationContext
;
}
//通过name获取 Bean.
public
static
Object
getBean
(
String
name
){
return
getApplicationContext
().
getBean
(
name
);
}
//通过class获取Bean.
public
static
<
T
>
T
getBean
(
Class
<
T
>
clazz
){
return
getApplicationContext
().
getBean
(
clazz
);
}
//通过name,以及Clazz返回指定的Bean
public
static
<
T
>
T
getBean
(
String
name
,
Class
<
T
>
clazz
){
return
getApplicationContext
().
getBean
(
name
,
clazz
);
}
}
pom.xml
View file @
7132ad8e
...
@@ -83,6 +83,12 @@
...
@@ -83,6 +83,12 @@
<scope>
import
</scope>
<scope>
import
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-aop
</artifactId>
<version>
${spring.boot.version}
</version>
</dependency>
<dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-dependencies
</artifactId>
<artifactId>
spring-cloud-dependencies
</artifactId>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment