diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..5c871a4 --- /dev/null +++ b/.github/workflows/go.yml @@ -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 ./... diff --git a/ttl/ttl.go b/ttl/ttl.go index d8765a3..7164c5f 100644 --- a/ttl/ttl.go +++ b/ttl/ttl.go @@ -4,6 +4,8 @@ import ( "context" "sync" "time" + + "github.com/google/uuid" ) // TTL represents a time-based cache system with sharding and dynamic eviction. @@ -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] @@ -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 { @@ -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 @@ -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()