Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bobgroup-go-utils
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bob Public Utils
bobgroup-go-utils
Commits
746ba073
Commit
746ba073
authored
1 year ago
by
Jano Hendriks
Browse files
Options
Downloads
Patches
Plain Diff
Allow updating redis key expiry when getting values
parent
a1f9cdfe
Branches
Branches containing commit
Tags
v1.225.0
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
redis/redis.go
+14
-4
14 additions, 4 deletions
redis/redis.go
with
14 additions
and
4 deletions
redis/redis.go
+
14
−
4
View file @
746ba073
...
@@ -184,14 +184,16 @@ func (r ClientWithHelpers) SetObjectByKeyIndefinitely(key string, object interfa
...
@@ -184,14 +184,16 @@ func (r ClientWithHelpers) SetObjectByKeyIndefinitely(key string, object interfa
}
}
func
GetObjectByKey
[
T
any
](
redisClient
*
ClientWithHelpers
,
key
string
,
object
T
)
*
T
{
// GetObjectByKey fetches an object from Redis by key. If the key does not exist or there is an error, it returns nil.
// If an expiry is provided, it will both fetch the key and update its expiry.
func
GetObjectByKey
[
T
any
](
redisClient
*
ClientWithHelpers
,
key
string
,
object
T
,
expiry
...
time
.
Duration
)
*
T
{
// Make sure we have a Redis client, and it is connected
// Make sure we have a Redis client, and it is connected
if
redisClient
==
nil
||
!
redisClient
.
IsConnected
()
{
if
redisClient
==
nil
||
!
redisClient
.
IsConnected
()
{
return
nil
return
nil
}
}
// Get the object from Redis
// Get the object from Redis
jsonString
:=
redisClient
.
GetValueByKey
(
key
)
jsonString
:=
redisClient
.
GetValueByKey
(
key
,
expiry
...
)
if
jsonString
==
""
{
if
jsonString
==
""
{
return
nil
return
nil
}
}
...
@@ -205,12 +207,20 @@ func GetObjectByKey[T any](redisClient *ClientWithHelpers, key string, object T)
...
@@ -205,12 +207,20 @@ func GetObjectByKey[T any](redisClient *ClientWithHelpers, key string, object T)
return
&
object
return
&
object
}
}
func
(
r
ClientWithHelpers
)
GetValueByKey
(
key
string
)
string
{
// GetValueByKey fetches a value from Redis by key. If the key does not exist or there is an error, it returns an empty
// string. If an expiry is provided, it will both fetch the key and update its expiry.
func
(
r
ClientWithHelpers
)
GetValueByKey
(
key
string
,
expiry
...
time
.
Duration
)
string
{
if
!
r
.
IsConnected
()
{
if
!
r
.
IsConnected
()
{
return
""
return
""
}
}
jsonString
,
err
:=
r
.
Client
.
Get
(
ctx
,
key
)
.
Result
()
var
jsonString
string
var
err
error
if
len
(
expiry
)
>
0
{
jsonString
,
err
=
r
.
Client
.
GetEx
(
ctx
,
key
,
expiry
[
0
])
.
Result
()
}
else
{
jsonString
,
err
=
r
.
Client
.
Get
(
ctx
,
key
)
.
Result
()
}
if
err
==
redis
.
Nil
{
/* Key does not exist */
if
err
==
redis
.
Nil
{
/* Key does not exist */
return
""
return
""
}
else
if
err
!=
nil
{
/* Actual error */
}
else
if
err
!=
nil
{
/* Actual error */
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment