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
8520e307
Commit
8520e307
authored
May 27, 2025
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: handle type assertion safely
parent
ef6f80d9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
9 deletions
+23
-9
cache.go
store/cache/cache.go
+23
-9
No files found.
store/cache/cache.go
View file @
8520e307
...
...
@@ -152,8 +152,9 @@ func (c *Cache) Delete(_ context.Context, key string) {
atomic
.
AddInt64
(
&
c
.
itemCount
,
-
1
)
if
c
.
config
.
OnEviction
!=
nil
{
itm
,
_
:=
value
.
(
item
)
c
.
config
.
OnEviction
(
key
,
itm
.
value
)
if
itm
,
ok
:=
value
.
(
item
);
ok
{
c
.
config
.
OnEviction
(
key
,
itm
.
value
)
}
}
}
}
...
...
@@ -162,8 +163,13 @@ func (c *Cache) Delete(_ context.Context, key string) {
func
(
c
*
Cache
)
Clear
(
_
context
.
Context
)
{
if
c
.
config
.
OnEviction
!=
nil
{
c
.
data
.
Range
(
func
(
key
,
value
any
)
bool
{
itm
,
_
:=
value
.
(
item
)
c
.
config
.
OnEviction
(
key
.
(
string
),
itm
.
value
)
itm
,
ok
:=
value
.
(
item
)
if
!
ok
{
return
true
}
if
keyStr
,
ok
:=
key
.
(
string
);
ok
{
c
.
config
.
OnEviction
(
keyStr
,
itm
.
value
)
}
return
true
})
}
...
...
@@ -214,13 +220,18 @@ func (c *Cache) cleanup() {
count
:=
0
c
.
data
.
Range
(
func
(
key
,
value
any
)
bool
{
itm
,
_
:=
value
.
(
item
)
itm
,
ok
:=
value
.
(
item
)
if
!
ok
{
return
true
}
if
time
.
Now
()
.
After
(
itm
.
expiration
)
{
c
.
data
.
Delete
(
key
)
count
++
if
c
.
config
.
OnEviction
!=
nil
{
evicted
[
key
.
(
string
)]
=
itm
.
value
if
keyStr
,
ok
:=
key
.
(
string
);
ok
{
evicted
[
keyStr
]
=
itm
.
value
}
}
}
return
true
...
...
@@ -261,9 +272,12 @@ func (c *Cache) cleanupOldest() {
candidates
:=
make
([]
keyExpPair
,
0
,
threshold
)
c
.
data
.
Range
(
func
(
key
,
value
any
)
bool
{
itm
:=
value
.
(
item
)
if
len
(
candidates
)
<
threshold
{
candidates
=
append
(
candidates
,
keyExpPair
{
key
.
(
string
),
itm
.
value
,
itm
.
expiration
})
itm
,
ok
:=
value
.
(
item
)
if
!
ok
{
return
true
}
if
keyStr
,
ok
:=
key
.
(
string
);
ok
&&
len
(
candidates
)
<
threshold
{
candidates
=
append
(
candidates
,
keyExpPair
{
keyStr
,
itm
.
value
,
itm
.
expiration
})
return
true
}
...
...
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