Skip to content

Commit 74683b1

Browse files
Merge pull request #61 from dscsnu/db-cache-layer
Db cache layer
2 parents f5adc38 + d46e7f2 commit 74683b1

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

backend/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/jackc/puddle/v2 v2.2.2 // indirect
2424
github.com/josharian/intern v1.0.0 // indirect
2525
github.com/mailru/easyjson v0.9.0 // indirect
26+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
2627
github.com/stretchr/testify v1.10.0 // indirect
2728
github.com/swaggo/files v0.0.0-20220610200504-28940afbdbfe // indirect
2829
github.com/swaggo/files/v2 v2.0.0 // indirect

backend/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFF
3434
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
3535
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
3636
github.com/mailru/easyjson v0.9.0/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU=
37+
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
38+
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
3739
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3840
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3941
github.com/rs/cors v1.11.1 h1:eU3gRzXLRK57F5rKMGMZURNdIG4EoAmX8k94r9wXWHA=

backend/internal/cache/cache.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cache
2+
3+
import (
4+
"time"
5+
6+
"github.com/patrickmn/go-cache"
7+
)
8+
9+
type Cache struct {
10+
c *cache.Cache
11+
}
12+
13+
func New(defaultExpiration, cleanupInterval time.Duration) *Cache {
14+
return &Cache{
15+
c: cache.New(defaultExpiration, cleanupInterval),
16+
}
17+
}
18+
19+
func (c *Cache) Set(key string, value interface{}, expiration time.Duration) {
20+
c.c.Set(key, value, expiration)
21+
}
22+
23+
func (c *Cache) Get(key string) (interface{}, bool) {
24+
return c.c.Get(key)
25+
}
26+
27+
func (c *Cache) Delete(key string) {
28+
c.c.Delete(key)
29+
}
30+
31+
func (c *Cache) Flush() {
32+
c.c.Flush()
33+
}
34+
35+
func (c *Cache) SetTTL(defaultExpiration, cleanupInterval time.Duration) {
36+
c.c = cache.New(defaultExpiration, cleanupInterval)
37+
}
38+
39+
func DefaultExpiration() time.Duration {
40+
return cache.DefaultExpiration
41+
}
42+
43+
func NoExpiration() time.Duration {
44+
return cache.NoExpiration
45+
}

backend/internal/database/database.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package database
33
import (
44
"context"
55
"crypto/rand"
6+
"labyrinth/internal/cache"
67
"math/big"
8+
"time"
79

810
"github.com/google/uuid"
911
"github.com/jackc/pgx/v5/pgtype"
@@ -30,7 +32,8 @@ func genRand() (string, error) {
3032
}
3133

3234
type PostgresDriver struct {
33-
pool *pgxpool.Pool
35+
pool *pgxpool.Pool
36+
cache *cache.Cache
3437
}
3538

3639
func (pd *PostgresDriver) Close() {
@@ -42,5 +45,11 @@ func CreatePostgresDriver(connectionURL string) (*PostgresDriver, error) {
4245
if err != nil {
4346
return nil, err
4447
}
45-
return &PostgresDriver{pool: pool}, nil
48+
49+
c := cache.New(5*time.Minute, 10*time.Minute)
50+
51+
return &PostgresDriver{
52+
pool: pool,
53+
cache: c,
54+
}, nil
4655
}

backend/internal/database/team_database.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ func (pd *PostgresDriver) LeaveTeamMember(ctx context.Context, teamId string, us
8585
}
8686

8787
func (pd *PostgresDriver) GetTeamByID(ctx context.Context, teamId string) (types.Team, error) {
88+
cacheKey := "team:" + teamId
89+
90+
if cachedData, found := pd.cache.Get(cacheKey); found {
91+
if team, ok := cachedData.(types.Team); ok {
92+
return team, nil
93+
}
94+
}
95+
8896
team := types.Team{}
8997

9098
err := pd.pool.QueryRow(ctx, "SELECT id, name FROM team WHERE team.id=$1", teamId).Scan(&team.ID, &team.Name)
@@ -114,8 +122,10 @@ func (pd *PostgresDriver) GetTeamByID(ctx context.Context, teamId string) (types
114122
}
115123

116124
team.Members = members
117-
return team, nil
118125

126+
pd.cache.Set(cacheKey, team, 5*time.Minute)
127+
128+
return team, nil
119129
}
120130

121131
func (pd *PostgresDriver) GetTeamByUserId(ctx context.Context, userId uuid.UUID) (types.Team, error) {

0 commit comments

Comments
 (0)