aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authoralex <[email protected]>2026-07-06 14:38:56 +0200
committeralex <[email protected]>2026-07-06 14:38:56 +0200
commit316b443addb7ec55e27f571ef489f2c19afb3a82 (patch)
treeb22dc242186b85b888cde0da002c8c95c0adcc99 /main.go
parent48df6e2f7c36a859e7d76de23017eef84452c0ed (diff)
downloadredis-clone-316b443addb7ec55e27f571ef489f2c19afb3a82.tar.xz
redis-clone-316b443addb7ec55e27f571ef489f2c19afb3a82.zip
added struct Item and keyowrd EXPIRE so now we can track expire time of items in the db
Diffstat (limited to 'main.go')
-rw-r--r--main.go17
1 files changed, 16 insertions, 1 deletions
diff --git a/main.go b/main.go
index d225a52..ade1b76 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"net"
"os"
"strings"
+ "time"
)
@@ -27,10 +28,24 @@ var commandRegistry = map[string]handlerFunc{
"LRANGE": handleLrange,
"LPOP": handleLpop,
"RPOP": handleRpop,
+ "EXPIRE": handleExpire,
}
func main() {
- db = RedisDB{data: make(map[string]any)}
+ db = RedisDB{data: make(map[string]Item)}
+
+ 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)
+ }
+ }
+ db.mu.Unlock()
+ }
+ }()
listener, err := net.Listen("tcp", ":6379")
if err != nil {