Skip to content

chore: change chinese comment to english #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions agcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,51 @@

package agcache

// CacheInterface 自定义缓存组件接口
// CacheInterface defines the contract for custom cache implementations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, I feel like this translation might not fully capture the original meaning in Chinese.

type CacheInterface interface {
// Set stores a key-value pair in the cache with an expiration time
// Parameters:
// - key: The unique identifier for the cache entry
// - value: The data to be stored
// - expireSeconds: Time in seconds after which the entry should expire
// Returns:
// - error: Any error that occurred during the operation
Set(key string, value interface{}, expireSeconds int) (err error)

// EntryCount returns the total number of entries in the cache
// Returns:
// - entryCount: The current number of entries stored in the cache
EntryCount() (entryCount int64)

// Get retrieves a value from the cache by its key
// Parameters:
// - key: The unique identifier for the cache entry
// Returns:
// - value: The stored value if found
// - error: Error if the key doesn't exist or any other error occurs
Get(key string) (value interface{}, err error)

// Del removes an entry from the cache by its key
// Parameters:
// - key: The unique identifier of the entry to be deleted
// Returns:
// - affected: True if the key was found and deleted, false otherwise
Del(key string) (affected bool)

// Range iterates over all key/value pairs in the cache
// Parameters:
// - f: The function to be executed for each cache entry
// Return false from f to stop iteration
Range(f func(key, value interface{}) bool)

// Clear removes all entries from the cache
Clear()
}

// CacheFactory 缓存组件工厂接口
// CacheFactory defines the interface for creating cache instances
type CacheFactory interface {
//Create 创建缓存组件
// Create instantiates and returns a new cache implementation
// Returns:
// - CacheInterface: A new instance of a cache implementation
Create() CacheInterface
}
48 changes: 37 additions & 11 deletions agcache/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,41 @@ import (
"github.com/apolloconfig/agollo/v4/agcache"
)

// DefaultCache 默认缓存
// DefaultCache implements a thread-safe in-memory cache using sync.Map
type DefaultCache struct {
defaultCache sync.Map
count int64
defaultCache sync.Map // The underlying thread-safe map for storing cache entries
count int64 // Counter for tracking the number of cache entries
}

// Set 获取缓存
// Set stores a key-value pair in the cache
// Parameters:
// - key: The unique identifier for the cache entry
// - value: The data to be stored
// - expireSeconds: Time in seconds after which the entry should expire (currently not implemented)
//
// Returns:
// - error: Always returns nil as the operation cannot fail
func (d *DefaultCache) Set(key string, value interface{}, expireSeconds int) (err error) {
d.defaultCache.Store(key, value)
atomic.AddInt64(&d.count, int64(1))
return nil
}

// EntryCount 获取实体数量
// EntryCount returns the total number of entries in the cache
// Returns:
// - entryCount: The current number of entries stored in the cache
func (d *DefaultCache) EntryCount() (entryCount int64) {
c := atomic.LoadInt64(&d.count)
return c
}

// Get 获取缓存
// Get retrieves a value from the cache by its key
// Parameters:
// - key: The unique identifier for the cache entry
//
// Returns:
// - value: The stored value if found
// - error: Error if the key doesn't exist in the cache
func (d *DefaultCache) Get(key string) (value interface{}, err error) {
v, ok := d.defaultCache.Load(key)
if !ok {
Expand All @@ -50,29 +65,40 @@ func (d *DefaultCache) Get(key string) (value interface{}, err error) {
return v, nil
}

// Range 遍历缓存
// Range iterates over all key/value pairs in the cache
// Parameters:
// - f: The function to be executed for each cache entry
// Return false from f to stop iteration
func (d *DefaultCache) Range(f func(key, value interface{}) bool) {
d.defaultCache.Range(f)
}

// Del 删除缓存
// Del removes an entry from the cache by its key
// Parameters:
// - key: The unique identifier of the entry to be deleted
//
// Returns:
// - affected: Always returns true regardless of whether the key existed
func (d *DefaultCache) Del(key string) (affected bool) {
d.defaultCache.Delete(key)
atomic.AddInt64(&d.count, int64(-1))
return true
}

// Clear 清除所有缓存
// Clear removes all entries from the cache
// This operation reinitializes the underlying sync.Map and resets the counter
func (d *DefaultCache) Clear() {
d.defaultCache = sync.Map{}
atomic.StoreInt64(&d.count, int64(0))
}

// DefaultCacheFactory 构造默认缓存组件工厂类
// DefaultCacheFactory is a factory for creating new instances of DefaultCache
type DefaultCacheFactory struct {
}

// Create 创建默认缓存组件
// Create instantiates and returns a new DefaultCache instance
// Returns:
// - agcache.CacheInterface: A new instance of DefaultCache
func (d *DefaultCacheFactory) Create() agcache.CacheInterface {
return &DefaultCache{}
}
Loading
Loading