hanahmily commented on code in PR #670:
URL: 
https://github.com/apache/skywalking-banyandb/pull/670#discussion_r2163701583


##########
banyand/internal/storage/cache.go:
##########
@@ -0,0 +1,266 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package storage
+
+import (
+       "container/heap"
+       "sync"
+       "sync/atomic"
+       "time"
+       "unsafe"
+
+       "github.com/apache/skywalking-banyandb/api/common"
+       "github.com/apache/skywalking-banyandb/pkg/run"
+)
+
+// Cache encapsulates the cache operations.
+type Cache interface {
+       Get(key EntryKey) any
+       Put(key EntryKey, value any)
+       StartCleaner()
+       Close()
+       Requests() uint64
+       Misses() uint64
+       Entries() uint64
+       Size() uint64
+}
+
+// CacheConfig holds configuration parameters for the cache.
+type CacheConfig struct {
+       MaxCacheSize    run.Bytes
+       CleanupInterval time.Duration
+       IdleTimeout     time.Duration
+}
+
+// DefaultCacheConfig returns the default cache configuration.
+func DefaultCacheConfig() CacheConfig {
+       return CacheConfig{
+               MaxCacheSize:    run.Bytes(100 * 1024 * 1024),
+               CleanupInterval: 30 * time.Second,
+               IdleTimeout:     2 * time.Minute,
+       }
+}
+
+type entry struct {
+       value      any
+       lastAccess uint64
+}
+
+// EntryKey is the key of an entry in the cache.
+type EntryKey struct {
+       group     string
+       partID    uint64
+       offset    uint64
+       segmentID segmentID
+       shardID   common.ShardID
+}
+
+// NewEntryKey creates an entry key with partID and offset.
+func NewEntryKey(partID uint64, offset uint64) EntryKey {
+       return EntryKey{
+               partID: partID,
+               offset: offset,
+       }
+}
+
+type entryIndex struct {
+       *entry
+       key   EntryKey
+       index int
+}
+
+type entryIndexHeap []*entryIndex
+
+func (h entryIndexHeap) Len() int { return len(h) }
+
+func (h entryIndexHeap) Less(i, j int) bool {
+       return atomic.LoadUint64(&h[i].entry.lastAccess) < 
atomic.LoadUint64(&h[j].entry.lastAccess)
+}
+
+func (h entryIndexHeap) Swap(i, j int) {
+       h[i], h[j] = h[j], h[i]
+       h[i].index = i
+       h[j].index = j
+}
+
+func (h *entryIndexHeap) Push(x interface{}) {
+       n := len(*h)
+       ei := x.(*entryIndex)
+       ei.index = n
+       *h = append(*h, ei)
+}
+
+func (h *entryIndexHeap) Pop() interface{} {
+       old := *h
+       n := len(old)
+       x := old[n-1]
+       *h = old[:n-1]
+       return x
+}
+
+var _ Cache = (*serviceCache)(nil)
+
+type serviceCache struct {
+       entry           map[EntryKey]*entry
+       entryIndex      map[EntryKey]*entryIndex
+       entryIndexHeap  *entryIndexHeap
+       stopCh          chan struct{}
+       requests        uint64
+       misses          uint64
+       mu              sync.RWMutex
+       wg              sync.WaitGroup
+       maxCacheSize    uint64
+       cleanupInterval time.Duration
+       idleTimeout     time.Duration
+}
+
+// NewServiceCache creates a cache for service with default configuration.
+func NewServiceCache() Cache {
+       return NewServiceCacheWithConfig(DefaultCacheConfig())
+}
+
+// NewServiceCacheWithConfig creates a cache for service with custom 
configuration.
+func NewServiceCacheWithConfig(config CacheConfig) Cache {
+       h := &entryIndexHeap{}
+       heap.Init(h)
+       sc := &serviceCache{
+               entry:           make(map[EntryKey]*entry),
+               entryIndexHeap:  h,
+               entryIndex:      make(map[EntryKey]*entryIndex),
+               stopCh:          make(chan struct{}),
+               wg:              sync.WaitGroup{},
+               maxCacheSize:    uint64(config.MaxCacheSize),
+               cleanupInterval: config.CleanupInterval,
+               idleTimeout:     config.IdleTimeout,
+       }
+       sc.wg.Add(1)
+       sc.StartCleaner()
+       return sc
+}
+
+func (sc *serviceCache) StartCleaner() {

Review Comment:
   ```suggestion
        sc.wg.Add(1)
        sc.startCleaner()
        return sc
   }
   
   func (sc *serviceCache) startCleaner() {
   ```



##########
banyand/internal/storage/cache.go:
##########
@@ -0,0 +1,266 @@
+// Licensed to Apache Software Foundation (ASF) under one or more contributor
+// license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright
+// ownership. Apache Software Foundation (ASF) licenses this file to you under
+// the Apache License, Version 2.0 (the "License"); you may
+// not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package storage
+
+import (
+       "container/heap"
+       "sync"
+       "sync/atomic"
+       "time"
+       "unsafe"
+
+       "github.com/apache/skywalking-banyandb/api/common"
+       "github.com/apache/skywalking-banyandb/pkg/run"
+)
+
+// Cache encapsulates the cache operations.
+type Cache interface {
+       Get(key EntryKey) any
+       Put(key EntryKey, value any)
+       StartCleaner()

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@skywalking.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to