Skip to content

Commit

Permalink
Merge branch 'main' into ttl
Browse files Browse the repository at this point in the history
  • Loading branch information
ukashazia authored Feb 16, 2025
2 parents 52a4271 + afb1158 commit 8caaaae
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23.0'

- name: Build
run: go build -v ./...

- name: Test
run: go test -race -v ./...
8 changes: 8 additions & 0 deletions ttl/ttl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"sync"
"time"

"github.com/google/uuid"

Check failure on line 8 in ttl/ttl.go

View workflow job for this annotation

GitHub Actions / build

"github.com/google/uuid" imported and not used
)

// TTL represents a time-based cache system with sharding and dynamic eviction.
Expand Down Expand Up @@ -46,12 +48,14 @@ type shardLookupTable struct {
func (ttl *TTL) Init() error {
newShard := shard{}
ttl.shardLookupTable = shardLookupTable{shards: make(shards)}

ttl.newShard(&newShard)
return nil
}

// Put inserts an item into the cache and returns the shard ID it was stored in.
func (ttl *TTL) Put(item *Item) (uint64, error) {

ttl.shardLookupTable.mutex.RLock()
shardId := ttl.shardLookupTable.currentShardId
currentShard := ttl.shardLookupTable.shards[shardId]
Expand All @@ -64,7 +68,9 @@ func (ttl *TTL) Put(item *Item) (uint64, error) {
}

currentShard.mutex.Lock()

if uint64(len(currentShard.data)) < ttl.ShardSize && !currentShard.isTerminated && currentShard != nil {

currentShard.data[item.Key] = item
currentShard.mutex.Unlock()
} else {
Expand Down Expand Up @@ -162,6 +168,7 @@ func (shard *shard) cleanup(ctx context.Context, ttl *TTL) {
}

if len(shard.data) == 0 {

shard.mutex.Unlock()
ttl.terminateShard(shard)
return
Expand All @@ -173,6 +180,7 @@ func (shard *shard) cleanup(ctx context.Context, ttl *TTL) {

// terminateShard removes an empty shard from the lookup table.
func (ttl *TTL) terminateShard(shard *shard) {

ttl.shardLookupTable.mutex.Lock()
defer ttl.shardLookupTable.mutex.Unlock()

Expand Down

0 comments on commit 8caaaae

Please sign in to comment.