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
3556e454
Commit
3556e454
authored
Jun 11, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
redis cache
parent
c3c96356
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
109 additions
and
1 deletion
+109
-1
pom.xml
community-service/pom.xml
+11
-0
CommunityApplication.java
...c/main/java/com/tanpu/community/CommunityApplication.java
+2
-0
CacheConfig.java
...src/main/java/com/tanpu/community/config/CacheConfig.java
+66
-0
HomePageController.java
...va/com/tanpu/community/controller/HomePageController.java
+2
-0
FansRelService.java
...main/java/com/tanpu/community/service/FansRelService.java
+4
-1
application-dev.yml
community-service/src/main/resources/application-dev.yml
+12
-0
pom.xml
pom.xml
+12
-0
No files found.
community-service/pom.xml
View file @
3556e454
...
...
@@ -53,6 +53,17 @@
<!-- <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>-->
<!-- </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>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-openfeign
</artifactId>
...
...
community-service/src/main/java/com/tanpu/community/CommunityApplication.java
View file @
3556e454
...
...
@@ -2,11 +2,13 @@ package com.tanpu.community;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.autoconfigure.SpringBootApplication
;
import
org.springframework.cache.annotation.EnableCaching
;
import
org.springframework.context.annotation.ComponentScan
;
import
org.springframework.transaction.annotation.EnableTransactionManagement
;
@SpringBootApplication
@EnableTransactionManagement
@EnableCaching
@ComponentScan
({
"com.tanpu.common"
,
"com.tanpu.community"
})
public
class
CommunityApplication
{
...
...
community-service/src/main/java/com/tanpu/community/config/CacheConfig.java
0 → 100644
View file @
3556e454
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
,
"_"
);
}
}
}
community-service/src/main/java/com/tanpu/community/controller/HomePageController.java
View file @
3556e454
package
com
.
tanpu
.
community
.
controller
;
import
com.alibaba.fastjson.JSON
;
import
com.tanpu.community.dao.entity.community.FansRelEntity
;
import
com.tanpu.community.manager.HomePageManager
;
import
com.tanpu.community.model.req.homepage.AddIdolReq
;
...
...
@@ -24,6 +25,7 @@ public class HomePageController {
public
String
queryMyFans
()
{
String
userId
=
"123"
;
List
<
FansRelEntity
>
fans
=
homePageManager
.
queryFansByIdolId
(
userId
);
System
.
out
.
println
(
fans
.
size
());
return
null
;
}
...
...
community-service/src/main/java/com/tanpu/community/service/FansRelService.java
View file @
3556e454
...
...
@@ -3,11 +3,14 @@ package com.tanpu.community.service;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.tanpu.community.dao.entity.community.FansRelEntity
;
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
javax.annotation.Resource
;
import
java.util.List
;
@EnableCaching
@Service
public
class
FansRelService
{
...
...
@@ -18,12 +21,12 @@ public class FansRelService {
return
fansRelMapper
.
selectList
(
new
LambdaQueryWrapper
<
FansRelEntity
>().
eq
(
FansRelEntity:
:
getFollowerId
,
followerId
));
}
@Cacheable
(
value
=
"tempCache"
,
keyGenerator
=
"communityKeyGenerator"
)
public
List
<
FansRelEntity
>
queryFansByIdolId
(
String
idolId
)
{
return
fansRelMapper
.
selectList
(
new
LambdaQueryWrapper
<
FansRelEntity
>().
eq
(
FansRelEntity:
:
getIdolId
,
idolId
));
}
public
void
addFans
(
String
idolId
,
String
followerId
)
{
FansRelEntity
rel
=
new
FansRelEntity
();
rel
.
setIdolId
(
idolId
);
rel
.
setFollowerId
(
followerId
);
...
...
community-service/src/main/resources/application-dev.yml
View file @
3556e454
...
...
@@ -23,6 +23,18 @@ spring.datasource:
minIdle
:
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
:
sleuth
:
enabled
:
false
...
...
pom.xml
View file @
3556e454
...
...
@@ -55,6 +55,13 @@
<scope>
import
</scope>
</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>
<groupId>
com.baomidou
</groupId>
<artifactId>
mybatis-plus-boot-starter
</artifactId>
...
...
@@ -67,6 +74,11 @@
<version>
3.4.1
</version>
</dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<artifactId>
fastjson
</artifactId>
<version>
1.2.47
</version>
</dependency>
<dependency>
<groupId>
com.github.pagehelper
</groupId>
...
...
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