Skip to content

Commit fb3cfd5

Browse files
committed
feat: add healthcheck
1 parent 01dac8c commit fb3cfd5

9 files changed

+57
-66
lines changed

cache.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package multi_tier_caching
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"time"
78
)
89

@@ -133,3 +134,21 @@ func containsLayer(layers []CacheLayer, target CacheLayer) bool {
133134
}
134135
return false
135136
}
137+
138+
func (c *MultiTierCache) HealthCheck(ctx context.Context) error {
139+
// Checking all cache layers
140+
for _, layer := range c.layers {
141+
if err := layer.CheckHealth(ctx); err != nil {
142+
return fmt.Errorf("cache layer error: %w", err)
143+
}
144+
}
145+
146+
// Database check
147+
if db, ok := c.db.(HealthChecker); ok {
148+
if err := db.CheckHealth(ctx); err != nil {
149+
return fmt.Errorf("database error: %w", err)
150+
}
151+
}
152+
153+
return nil
154+
}

database.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ func (d *DatabaseCache) Delete(ctx context.Context, key string) {
4141
return
4242
}
4343
}
44+
45+
func (d *DatabaseCache) CheckHealth(ctx context.Context) error {
46+
return d.storage.CheckHealth(ctx)
47+
}

memory_cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ func (m *MemoryCache) Set(ctx context.Context, key string, value string, ttl tim
4848
func (m *MemoryCache) Delete(ctx context.Context, key string) {
4949
m.storage.Delete(ctx, key)
5050
}
51+
52+
func (r *MemoryCache) CheckHealth(ctx context.Context) error {
53+
return r.storage.CheckHealth(ctx)
54+
}

memory_cache_test.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

redis.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,7 @@ func (r *RedisCache) Set(ctx context.Context, key string, value string, ttl time
3434
func (r *RedisCache) Delete(ctx context.Context, key string) {
3535
r.storage.Delete(ctx, key)
3636
}
37+
38+
func (r *RedisCache) CheckHealth(ctx context.Context) error {
39+
return r.storage.CheckHealth(ctx)
40+
}

repository.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ type CacheLayer interface {
1010
Get(ctx context.Context, key string) (string, error)
1111
Set(ctx context.Context, key string, value string, ttl time.Duration) error
1212
Delete(ctx context.Context, key string)
13+
HealthChecker
1314
}
1415

1516
// Database — interface for working with the database
1617
type Database interface {
1718
Get(ctx context.Context, key string) (string, error)
1819
Set(ctx context.Context, key, value string, ttl time.Duration) error
1920
}
21+
22+
type HealthChecker interface {
23+
CheckHealth(ctx context.Context) error
24+
}

storage/database.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,12 @@ func (d *DatabaseStorage) DeleteCache(ctx context.Context, key string) error {
102102
func (d *DatabaseStorage) Close() {
103103
d.pool.Close()
104104
}
105+
106+
func (d *DatabaseStorage) CheckHealth(ctx context.Context) error {
107+
conn, err := d.pool.Acquire(ctx)
108+
if err != nil {
109+
return fmt.Errorf("database connection error: %w", err)
110+
}
111+
defer conn.Release()
112+
return conn.Ping(ctx)
113+
}

storage/redis.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ func (r *RedisStorage) Set(ctx context.Context, key string, value interface{}, t
4141
func (r *RedisStorage) Delete(ctx context.Context, key string) {
4242
r.client.Del(ctx, key)
4343
}
44+
45+
func (r *RedisStorage) CheckHealth(ctx context.Context) error {
46+
return r.client.Ping(ctx).Err()
47+
}

storage/ristretto.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ func (r *RistrettoCache) Set(ctx context.Context, key string, value string, ttl
5151
func (r *RistrettoCache) Delete(ctx context.Context, key string) {
5252
r.client.Del(key)
5353
}
54+
55+
func (r *RistrettoCache) CheckHealth(ctx context.Context) error {
56+
// Ristretto has no connection state, we only check for initialization
57+
if r.client == nil {
58+
return errors.New("ristretto cache is not initialized")
59+
}
60+
return nil
61+
}

0 commit comments

Comments
 (0)