@@ -22,26 +22,41 @@ import (
22
22
"github.com/apolloconfig/agollo/v4/agcache"
23
23
)
24
24
25
- // DefaultCache 默认缓存
25
+ // DefaultCache implements a thread-safe in-memory cache using sync.Map
26
26
type DefaultCache struct {
27
- defaultCache sync.Map
28
- count int64
27
+ defaultCache sync.Map // The underlying thread-safe map for storing cache entries
28
+ count int64 // Counter for tracking the number of cache entries
29
29
}
30
30
31
- // Set 获取缓存
31
+ // Set stores a key-value pair in the cache
32
+ // Parameters:
33
+ // - key: The unique identifier for the cache entry
34
+ // - value: The data to be stored
35
+ // - expireSeconds: Time in seconds after which the entry should expire (currently not implemented)
36
+ //
37
+ // Returns:
38
+ // - error: Always returns nil as the operation cannot fail
32
39
func (d * DefaultCache ) Set (key string , value interface {}, expireSeconds int ) (err error ) {
33
40
d .defaultCache .Store (key , value )
34
41
atomic .AddInt64 (& d .count , int64 (1 ))
35
42
return nil
36
43
}
37
44
38
- // EntryCount 获取实体数量
45
+ // EntryCount returns the total number of entries in the cache
46
+ // Returns:
47
+ // - entryCount: The current number of entries stored in the cache
39
48
func (d * DefaultCache ) EntryCount () (entryCount int64 ) {
40
49
c := atomic .LoadInt64 (& d .count )
41
50
return c
42
51
}
43
52
44
- // Get 获取缓存
53
+ // Get retrieves a value from the cache by its key
54
+ // Parameters:
55
+ // - key: The unique identifier for the cache entry
56
+ //
57
+ // Returns:
58
+ // - value: The stored value if found
59
+ // - error: Error if the key doesn't exist in the cache
45
60
func (d * DefaultCache ) Get (key string ) (value interface {}, err error ) {
46
61
v , ok := d .defaultCache .Load (key )
47
62
if ! ok {
@@ -50,29 +65,40 @@ func (d *DefaultCache) Get(key string) (value interface{}, err error) {
50
65
return v , nil
51
66
}
52
67
53
- // Range 遍历缓存
68
+ // Range iterates over all key/value pairs in the cache
69
+ // Parameters:
70
+ // - f: The function to be executed for each cache entry
71
+ // Return false from f to stop iteration
54
72
func (d * DefaultCache ) Range (f func (key , value interface {}) bool ) {
55
73
d .defaultCache .Range (f )
56
74
}
57
75
58
- // Del 删除缓存
76
+ // Del removes an entry from the cache by its key
77
+ // Parameters:
78
+ // - key: The unique identifier of the entry to be deleted
79
+ //
80
+ // Returns:
81
+ // - affected: Always returns true regardless of whether the key existed
59
82
func (d * DefaultCache ) Del (key string ) (affected bool ) {
60
83
d .defaultCache .Delete (key )
61
84
atomic .AddInt64 (& d .count , int64 (- 1 ))
62
85
return true
63
86
}
64
87
65
- // Clear 清除所有缓存
88
+ // Clear removes all entries from the cache
89
+ // This operation reinitializes the underlying sync.Map and resets the counter
66
90
func (d * DefaultCache ) Clear () {
67
91
d .defaultCache = sync.Map {}
68
92
atomic .StoreInt64 (& d .count , int64 (0 ))
69
93
}
70
94
71
- // DefaultCacheFactory 构造默认缓存组件工厂类
95
+ // DefaultCacheFactory is a factory for creating new instances of DefaultCache
72
96
type DefaultCacheFactory struct {
73
97
}
74
98
75
- // Create 创建默认缓存组件
99
+ // Create instantiates and returns a new DefaultCache instance
100
+ // Returns:
101
+ // - agcache.CacheInterface: A new instance of DefaultCache
76
102
func (d * DefaultCacheFactory ) Create () agcache.CacheInterface {
77
103
return & DefaultCache {}
78
104
}
0 commit comments