Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
71464779
Unverified
Commit
71464779
authored
Aug 03, 2025
by
Andrea Marchetta
Committed by
GitHub
Aug 03, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: change itemCount into an Int64 (#4945)
parent
07e27056
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
10 deletions
+11
-10
cache.go
store/cache/cache.go
+11
-10
No files found.
store/cache/cache.go
View file @
71464779
...
@@ -65,9 +65,9 @@ func DefaultConfig() Config {
...
@@ -65,9 +65,9 @@ func DefaultConfig() Config {
// Cache is a thread-safe in-memory cache with TTL and memory management.
// Cache is a thread-safe in-memory cache with TTL and memory management.
type
Cache
struct
{
type
Cache
struct
{
itemCount
atomic
.
Int64
// Use atomic operations to track item count
data
sync
.
Map
data
sync
.
Map
config
Config
config
Config
itemCount
int64
// Use atomic operations to track item count
stopChan
chan
struct
{}
stopChan
chan
struct
{}
closedChan
chan
struct
{}
closedChan
chan
struct
{}
}
}
...
@@ -104,7 +104,7 @@ func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Du
...
@@ -104,7 +104,7 @@ func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Du
c
.
data
.
Delete
(
key
)
c
.
data
.
Delete
(
key
)
}
else
{
}
else
{
// Only increment if this is a new key.
// Only increment if this is a new key.
atomic
.
AddInt64
(
&
c
.
itemCount
,
1
)
(
&
c
.
itemCount
)
.
Add
(
1
)
}
}
c
.
data
.
Store
(
key
,
item
{
c
.
data
.
Store
(
key
,
item
{
...
@@ -114,7 +114,7 @@ func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Du
...
@@ -114,7 +114,7 @@ func (c *Cache) SetWithTTL(_ context.Context, key string, value any, ttl time.Du
})
})
// If we're over the max items, clean up old items.
// If we're over the max items, clean up old items.
if
c
.
config
.
MaxItems
>
0
&&
atomic
.
LoadInt64
(
&
c
.
itemCount
)
>
int64
(
c
.
config
.
MaxItems
)
{
if
c
.
config
.
MaxItems
>
0
&&
(
&
c
.
itemCount
)
.
Load
(
)
>
int64
(
c
.
config
.
MaxItems
)
{
c
.
cleanupOldest
()
c
.
cleanupOldest
()
}
}
}
}
...
@@ -134,7 +134,7 @@ func (c *Cache) Get(_ context.Context, key string) (any, bool) {
...
@@ -134,7 +134,7 @@ func (c *Cache) Get(_ context.Context, key string) (any, bool) {
}
}
if
time
.
Now
()
.
After
(
itm
.
expiration
)
{
if
time
.
Now
()
.
After
(
itm
.
expiration
)
{
c
.
data
.
Delete
(
key
)
c
.
data
.
Delete
(
key
)
atomic
.
AddInt64
(
&
c
.
itemCount
,
-
1
)
(
&
c
.
itemCount
)
.
Add
(
-
1
)
if
c
.
config
.
OnEviction
!=
nil
{
if
c
.
config
.
OnEviction
!=
nil
{
c
.
config
.
OnEviction
(
key
,
itm
.
value
)
c
.
config
.
OnEviction
(
key
,
itm
.
value
)
...
@@ -149,7 +149,7 @@ func (c *Cache) Get(_ context.Context, key string) (any, bool) {
...
@@ -149,7 +149,7 @@ func (c *Cache) Get(_ context.Context, key string) (any, bool) {
// Delete removes a value from the cache.
// Delete removes a value from the cache.
func
(
c
*
Cache
)
Delete
(
_
context
.
Context
,
key
string
)
{
func
(
c
*
Cache
)
Delete
(
_
context
.
Context
,
key
string
)
{
if
value
,
loaded
:=
c
.
data
.
LoadAndDelete
(
key
);
loaded
{
if
value
,
loaded
:=
c
.
data
.
LoadAndDelete
(
key
);
loaded
{
atomic
.
AddInt64
(
&
c
.
itemCount
,
-
1
)
(
&
c
.
itemCount
)
.
Add
(
-
1
)
if
c
.
config
.
OnEviction
!=
nil
{
if
c
.
config
.
OnEviction
!=
nil
{
if
itm
,
ok
:=
value
.
(
item
);
ok
{
if
itm
,
ok
:=
value
.
(
item
);
ok
{
...
@@ -175,12 +175,12 @@ func (c *Cache) Clear(_ context.Context) {
...
@@ -175,12 +175,12 @@ func (c *Cache) Clear(_ context.Context) {
}
}
c
.
data
=
sync
.
Map
{}
c
.
data
=
sync
.
Map
{}
atomic
.
StoreInt64
(
&
c
.
itemCount
,
0
)
(
&
c
.
itemCount
)
.
Store
(
0
)
}
}
// Size returns the number of items in the cache.
// Size returns the number of items in the cache.
func
(
c
*
Cache
)
Size
()
int64
{
func
(
c
*
Cache
)
Size
()
int64
{
return
atomic
.
LoadInt64
(
&
c
.
itemCount
)
return
(
&
c
.
itemCount
)
.
Load
(
)
}
}
// Close stops the cache cleanup goroutine.
// Close stops the cache cleanup goroutine.
...
@@ -238,7 +238,7 @@ func (c *Cache) cleanup() {
...
@@ -238,7 +238,7 @@ func (c *Cache) cleanup() {
})
})
if
count
>
0
{
if
count
>
0
{
atomic
.
AddInt64
(
&
c
.
itemCount
,
-
int64
(
count
))
(
&
c
.
itemCount
)
.
Add
(
-
int64
(
count
))
// Call eviction callbacks outside the loop to avoid blocking the range
// Call eviction callbacks outside the loop to avoid blocking the range
if
c
.
config
.
OnEviction
!=
nil
{
if
c
.
config
.
OnEviction
!=
nil
{
...
@@ -254,7 +254,7 @@ func (c *Cache) cleanupOldest() {
...
@@ -254,7 +254,7 @@ func (c *Cache) cleanupOldest() {
// Remove 20% of max items at once
// Remove 20% of max items at once
threshold
:=
max
(
c
.
config
.
MaxItems
/
5
,
1
)
threshold
:=
max
(
c
.
config
.
MaxItems
/
5
,
1
)
currentCount
:=
atomic
.
LoadInt64
(
&
c
.
itemCount
)
currentCount
:=
(
&
c
.
itemCount
)
.
Load
(
)
// If we're not over the threshold, don't do anything
// If we're not over the threshold, don't do anything
if
currentCount
<=
int64
(
c
.
config
.
MaxItems
)
{
if
currentCount
<=
int64
(
c
.
config
.
MaxItems
)
{
...
@@ -308,7 +308,8 @@ func (c *Cache) cleanupOldest() {
...
@@ -308,7 +308,8 @@ func (c *Cache) cleanupOldest() {
// Update count
// Update count
if
deletedCount
>
0
{
if
deletedCount
>
0
{
atomic
.
AddInt64
(
&
c
.
itemCount
,
-
int64
(
deletedCount
))
(
&
c
.
itemCount
)
.
Add
(
-
int64
(
deletedCount
))
}
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment