Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
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
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
c001cfe1
Commit
c001cfe1
authored
Mar 14, 2019
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dont allow inifinite expiration
parent
51862737
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
22 additions
and
33 deletions
+22
-33
pkg/infra/remotecache/database_storage.go
+4
-4
pkg/infra/remotecache/memcached_storage.go
+6
-1
pkg/infra/remotecache/redis_storage.go
+1
-7
pkg/infra/remotecache/remotecache.go
+10
-1
pkg/infra/remotecache/remotecache_test.go
+1
-20
No files found.
pkg/infra/remotecache/database_storage.go
View file @
c001cfe1
...
@@ -92,18 +92,18 @@ func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration
...
@@ -92,18 +92,18 @@ func (dc *databaseCache) Set(key string, value interface{}, expire time.Duration
return
err
return
err
}
}
var
expires
AtEpoch
int64
var
expires
InSeconds
int64
if
expire
!=
0
{
if
expire
!=
0
{
expires
AtEpoch
=
int64
(
expire
)
/
int64
(
time
.
Second
)
expires
InSeconds
=
int64
(
expire
)
/
int64
(
time
.
Second
)
}
}
// insert or update depending on if item already exist
// insert or update depending on if item already exist
if
has
{
if
has
{
sql
:=
`UPDATE cache_data SET data=?, created=?, expire=? WHERE cache_key='?'`
sql
:=
`UPDATE cache_data SET data=?, created=?, expire=? WHERE cache_key='?'`
_
,
err
=
session
.
Exec
(
sql
,
data
,
getTime
()
.
Unix
(),
expires
AtEpoch
,
key
)
_
,
err
=
session
.
Exec
(
sql
,
data
,
getTime
()
.
Unix
(),
expires
InSeconds
,
key
)
}
else
{
}
else
{
sql
:=
`INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)`
sql
:=
`INSERT INTO cache_data (cache_key,data,created_at,expires) VALUES(?,?,?,?)`
_
,
err
=
session
.
Exec
(
sql
,
key
,
data
,
getTime
()
.
Unix
(),
expires
AtEpoch
)
_
,
err
=
session
.
Exec
(
sql
,
key
,
data
,
getTime
()
.
Unix
(),
expires
InSeconds
)
}
}
return
err
return
err
...
...
pkg/infra/remotecache/memcached_storage.go
View file @
c001cfe1
...
@@ -33,7 +33,12 @@ func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duratio
...
@@ -33,7 +33,12 @@ func (s *memcachedStorage) Set(key string, val interface{}, expires time.Duratio
return
err
return
err
}
}
memcachedItem
:=
newItem
(
key
,
bytes
,
int32
(
expires
))
var
expiresInSeconds
int64
if
expires
!=
0
{
expiresInSeconds
=
int64
(
expires
)
/
int64
(
time
.
Second
)
}
memcachedItem
:=
newItem
(
key
,
bytes
,
int32
(
expiresInSeconds
))
return
s
.
c
.
Set
(
memcachedItem
)
return
s
.
c
.
Set
(
memcachedItem
)
}
}
...
...
pkg/infra/remotecache/redis_storage.go
View file @
c001cfe1
...
@@ -27,13 +27,7 @@ func (s *redisStorage) Set(key string, val interface{}, expires time.Duration) e
...
@@ -27,13 +27,7 @@ func (s *redisStorage) Set(key string, val interface{}, expires time.Duration) e
return
err
return
err
}
}
var
status
*
redis
.
StatusCmd
status
:=
s
.
c
.
SetEx
(
key
,
expires
,
string
(
value
))
if
expires
==
0
{
status
=
s
.
c
.
Set
(
key
,
string
(
value
))
}
else
{
status
=
s
.
c
.
SetEx
(
key
,
expires
,
string
(
value
))
}
return
status
.
Err
()
return
status
.
Err
()
}
}
...
...
pkg/infra/remotecache/remotecache.go
View file @
c001cfe1
...
@@ -21,6 +21,8 @@ var (
...
@@ -21,6 +21,8 @@ var (
// ErrInvalidCacheType is returned if the type is invalid
// ErrInvalidCacheType is returned if the type is invalid
ErrInvalidCacheType
=
errors
.
New
(
"invalid remote cache name"
)
ErrInvalidCacheType
=
errors
.
New
(
"invalid remote cache name"
)
defaultMaxCacheExpiration
=
time
.
Hour
*
24
)
)
func
init
()
{
func
init
()
{
...
@@ -35,7 +37,7 @@ type CacheStorage interface {
...
@@ -35,7 +37,7 @@ type CacheStorage interface {
// Get reads object from Cache
// Get reads object from Cache
Get
(
key
string
)
(
interface
{},
error
)
Get
(
key
string
)
(
interface
{},
error
)
// Set sets an object into the cache. if `expire` is set to zero it
never expires.
// Set sets an object into the cache. if `expire` is set to zero it
will default to 24h
Set
(
key
string
,
value
interface
{},
expire
time
.
Duration
)
error
Set
(
key
string
,
value
interface
{},
expire
time
.
Duration
)
error
// Delete object from cache
// Delete object from cache
...
@@ -50,14 +52,21 @@ type RemoteCache struct {
...
@@ -50,14 +52,21 @@ type RemoteCache struct {
Cfg
*
setting
.
Cfg
`inject:""`
Cfg
*
setting
.
Cfg
`inject:""`
}
}
// Get reads object from Cache
func
(
ds
*
RemoteCache
)
Get
(
key
string
)
(
interface
{},
error
)
{
func
(
ds
*
RemoteCache
)
Get
(
key
string
)
(
interface
{},
error
)
{
return
ds
.
client
.
Get
(
key
)
return
ds
.
client
.
Get
(
key
)
}
}
// Set sets an object into the cache. if `expire` is set to zero it will default to 24h
func
(
ds
*
RemoteCache
)
Set
(
key
string
,
value
interface
{},
expire
time
.
Duration
)
error
{
func
(
ds
*
RemoteCache
)
Set
(
key
string
,
value
interface
{},
expire
time
.
Duration
)
error
{
if
expire
==
0
{
expire
=
defaultMaxCacheExpiration
}
return
ds
.
client
.
Set
(
key
,
value
,
expire
)
return
ds
.
client
.
Set
(
key
,
value
,
expire
)
}
}
// Delete object from cache
func
(
ds
*
RemoteCache
)
Delete
(
key
string
)
error
{
func
(
ds
*
RemoteCache
)
Delete
(
key
string
)
error
{
return
ds
.
client
.
Delete
(
key
)
return
ds
.
client
.
Delete
(
key
)
}
}
...
...
pkg/infra/remotecache/remotecache_test.go
View file @
c001cfe1
...
@@ -34,7 +34,7 @@ func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore *
...
@@ -34,7 +34,7 @@ func createTestClient(t *testing.T, opts *setting.RemoteCacheOptions, sqlstore *
t
.
Fatalf
(
"failed to init client for test. error: %v"
,
err
)
t
.
Fatalf
(
"failed to init client for test. error: %v"
,
err
)
}
}
return
dc
.
client
return
dc
}
}
func
TestCachedBasedOnConfig
(
t
*
testing
.
T
)
{
func
TestCachedBasedOnConfig
(
t
*
testing
.
T
)
{
...
@@ -56,7 +56,6 @@ func TestInvalidCacheTypeReturnsError(t *testing.T) {
...
@@ -56,7 +56,6 @@ func TestInvalidCacheTypeReturnsError(t *testing.T) {
func
runTestsForClient
(
t
*
testing
.
T
,
client
CacheStorage
)
{
func
runTestsForClient
(
t
*
testing
.
T
,
client
CacheStorage
)
{
canPutGetAndDeleteCachedObjects
(
t
,
client
)
canPutGetAndDeleteCachedObjects
(
t
,
client
)
canNotFetchExpiredItems
(
t
,
client
)
canNotFetchExpiredItems
(
t
,
client
)
canSetInfiniteCacheExpiration
(
t
,
client
)
}
}
func
canPutGetAndDeleteCachedObjects
(
t
*
testing
.
T
,
client
CacheStorage
)
{
func
canPutGetAndDeleteCachedObjects
(
t
*
testing
.
T
,
client
CacheStorage
)
{
...
@@ -92,21 +91,3 @@ func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
...
@@ -92,21 +91,3 @@ func canNotFetchExpiredItems(t *testing.T, client CacheStorage) {
_
,
err
=
client
.
Get
(
"key1"
)
_
,
err
=
client
.
Get
(
"key1"
)
assert
.
Equal
(
t
,
err
,
ErrCacheItemNotFound
)
assert
.
Equal
(
t
,
err
,
ErrCacheItemNotFound
)
}
}
func
canSetInfiniteCacheExpiration
(
t
*
testing
.
T
,
client
CacheStorage
)
{
cacheableStruct
:=
CacheableStruct
{
String
:
"hej"
,
Int64
:
2000
}
// insert cache item one day back
getTime
=
func
()
time
.
Time
{
return
time
.
Now
()
.
AddDate
(
0
,
0
,
-
2
)
}
err
:=
client
.
Set
(
"key1"
,
cacheableStruct
,
0
)
assert
.
Equal
(
t
,
err
,
nil
)
// should not be able to read that value since its expired
getTime
=
time
.
Now
data
,
err
:=
client
.
Get
(
"key1"
)
s
,
ok
:=
data
.
(
CacheableStruct
)
assert
.
Equal
(
t
,
ok
,
true
)
assert
.
Equal
(
t
,
s
.
String
,
"hej"
)
assert
.
Equal
(
t
,
s
.
Int64
,
int64
(
2000
))
}
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