aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-09 18:43:17 +0200
committeralex <[email protected]>2026-07-09 18:43:17 +0200
commitb845271ad2e64bc128e3ef6a419d898edd4911c1 (patch)
treeae5aba075e1e5195b8dbf0d6a615accdfc87ebc5 /main.go
parent8126486c8e488a6f541fb8b3bc65156fa983c489 (diff)
downloadredis-clone-b845271ad2e64bc128e3ef6a419d898edd4911c1.tar.xz
redis-clone-b845271ad2e64bc128e3ef6a419d898edd4911c1.zip
updated go routine for clearing expired keys
Diffstat (limited to 'main.go')
-rw-r--r--main.go17
1 files changed, 10 insertions, 7 deletions
diff --git a/main.go b/main.go
index 2364d02..54f2b38 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"net"
"os"
"strings"
+ "time"
)
const NumShards = 16
@@ -52,20 +53,22 @@ func main() {
}
}
db = RedisDB{shards: shards}
-/*
+
go func() {
ticker := time.NewTicker(1 * time.Second)
for range ticker.C {
- db.mu.Lock()
- for k, v := range db.data {
- if v.ExpiresAt != nil && time.Now().After(*v.ExpiresAt) {
- delete(db.data, k)
+ for _,shard := range db.shards{
+ shard.mu.Lock()
+ for k, v := range shard.data {
+ if v.ExpiresAt != nil && time.Now().After(*v.ExpiresAt) {
+ delete(shard.data, k)
+ }
}
+ shard.mu.Unlock()
}
- db.mu.Unlock()
}
}()
-*/
+
listener, err := net.Listen("tcp", ":6379")
if err != nil {
fmt.Println("error starting server: ", err)