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
c8ff698d
Commit
c8ff698d
authored
Feb 23, 2019
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
avoid exposing internal structs and functions
parent
3890bd14
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
47 deletions
+28
-47
pkg/infra/distcache/database_storage.go
+8
-8
pkg/infra/distcache/distcache.go
+5
-7
pkg/infra/distcache/distcache_test.go
+5
-9
pkg/infra/distcache/memcached_storage.go
+6
-6
pkg/infra/distcache/redis_storage.go
+4
-17
No files found.
pkg/infra/distcache/database_storage.go
View file @
c8ff698d
...
...
@@ -45,13 +45,13 @@ func (dc *databaseCache) StartGC() {
}
func
(
dc
*
databaseCache
)
Get
(
key
string
)
(
interface
{},
error
)
{
cacheHits
:=
[]
C
acheData
{}
cacheHits
:=
[]
c
acheData
{}
err
:=
dc
.
SQLStore
.
NewSession
()
.
Where
(
`key = ?`
,
key
)
.
Find
(
&
cacheHits
)
if
err
!=
nil
{
return
nil
,
err
}
var
cacheHit
C
acheData
var
cacheHit
c
acheData
if
len
(
cacheHits
)
==
0
{
return
nil
,
ErrCacheItemNotFound
}
...
...
@@ -64,15 +64,15 @@ func (dc *databaseCache) Get(key string) (interface{}, error) {
}
}
item
:=
&
Item
{}
if
err
=
D
ecodeGob
(
cacheHit
.
Data
,
item
);
err
!=
nil
{
item
:=
&
cached
Item
{}
if
err
=
d
ecodeGob
(
cacheHit
.
Data
,
item
);
err
!=
nil
{
return
nil
,
err
}
return
item
.
Val
,
nil
}
type
C
acheData
struct
{
type
c
acheData
struct
{
Key
string
Data
[]
byte
Expires
int64
...
...
@@ -80,15 +80,15 @@ type CacheData struct {
}
func
(
dc
*
databaseCache
)
Put
(
key
string
,
value
interface
{},
expire
time
.
Duration
)
error
{
item
:=
&
Item
{
Val
:
value
}
data
,
err
:=
E
ncodeGob
(
item
)
item
:=
&
cached
Item
{
Val
:
value
}
data
,
err
:=
e
ncodeGob
(
item
)
if
err
!=
nil
{
return
err
}
now
:=
getTime
()
.
Unix
()
cacheHits
:=
[]
C
acheData
{}
cacheHits
:=
[]
c
acheData
{}
err
=
dc
.
SQLStore
.
NewSession
()
.
Where
(
`key = ?`
,
key
)
.
Find
(
&
cacheHits
)
if
err
!=
nil
{
return
err
...
...
pkg/infra/distcache/distcache.go
View file @
c8ff698d
...
...
@@ -46,7 +46,7 @@ func createClient(opts CacheOpts, sqlstore *sqlstore.SqlStore) cacheStorage {
// return nil
// }
return
newDatabaseCache
(
sqlstore
)
//&databaseCache{SQLStore: sqlstore}
return
newDatabaseCache
(
sqlstore
)
}
// DistributedCache allows Grafana to cache data outside its own process
...
...
@@ -56,19 +56,17 @@ type DistributedCache struct {
SQLStore
*
sqlstore
.
SqlStore
`inject:""`
}
type
Item
struct
{
Val
interface
{}
Created
int64
Expire
int64
type
cachedItem
struct
{
Val
interface
{}
}
func
EncodeGob
(
item
*
Item
)
([]
byte
,
error
)
{
func
encodeGob
(
item
*
cached
Item
)
([]
byte
,
error
)
{
buf
:=
bytes
.
NewBuffer
(
nil
)
err
:=
gob
.
NewEncoder
(
buf
)
.
Encode
(
item
)
return
buf
.
Bytes
(),
err
}
func
DecodeGob
(
data
[]
byte
,
out
*
Item
)
error
{
func
decodeGob
(
data
[]
byte
,
out
*
cached
Item
)
error
{
buf
:=
bytes
.
NewBuffer
(
data
)
return
gob
.
NewDecoder
(
buf
)
.
Decode
(
&
out
)
}
...
...
pkg/infra/distcache/distcache_test.go
View file @
c8ff698d
...
...
@@ -27,7 +27,7 @@ func createTestClient(t *testing.T, name string) cacheStorage {
}
func
TestAllCacheClients
(
t
*
testing
.
T
)
{
clients
:=
[]
string
{
"database"
,
"redis"
,
"memcached"
}
// add redis, memcache, memory
clients
:=
[]
string
{
"database"
,
"redis"
}
// add redis, memcache, memory
for
_
,
v
:=
range
clients
{
client
:=
createTestClient
(
t
,
v
)
...
...
@@ -59,19 +59,15 @@ func CanPutGetAndDeleteCachedObjects(t *testing.T, name string, client cacheStor
}
func
CanNotFetchExpiredItems
(
t
*
testing
.
T
,
name
string
,
client
cacheStorage
)
{
if
name
==
"redis"
{
t
.
Skip
()
//this test does not work with redis since it uses its own getTime fn
}
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
.
Put
(
"key"
,
cacheableStruct
,
10000
*
time
.
Second
)
err
:=
client
.
Put
(
"key"
,
cacheableStruct
,
time
.
Second
)
assert
.
Equal
(
t
,
err
,
nil
)
//not sure how this can be avoided when testing redis/memcached :/
<-
time
.
After
(
time
.
Second
+
time
.
Millisecond
)
// should not be able to read that value since its expired
getTime
=
time
.
Now
_
,
err
=
client
.
Get
(
"key"
)
assert
.
Equal
(
t
,
err
,
ErrCacheItemNotFound
)
}
...
...
pkg/infra/distcache/memcached_storage.go
View file @
c8ff698d
...
...
@@ -16,7 +16,7 @@ func newMemcacheStorage(connStr string) *memcacheStorage {
}
}
func
N
ewItem
(
sid
string
,
data
[]
byte
,
expire
int32
)
*
memcache
.
Item
{
func
n
ewItem
(
sid
string
,
data
[]
byte
,
expire
int32
)
*
memcache
.
Item
{
return
&
memcache
.
Item
{
Key
:
sid
,
Value
:
data
,
...
...
@@ -26,14 +26,14 @@ func NewItem(sid string, data []byte, expire int32) *memcache.Item {
// Set sets value to given key in the cache.
func
(
s
*
memcacheStorage
)
Put
(
key
string
,
val
interface
{},
expires
time
.
Duration
)
error
{
item
:=
&
Item
{
Val
:
val
}
item
:=
&
cached
Item
{
Val
:
val
}
bytes
,
err
:=
E
ncodeGob
(
item
)
bytes
,
err
:=
e
ncodeGob
(
item
)
if
err
!=
nil
{
return
err
}
memcacheItem
:=
N
ewItem
(
key
,
bytes
,
int32
(
expires
))
memcacheItem
:=
n
ewItem
(
key
,
bytes
,
int32
(
expires
))
s
.
c
.
Add
(
memcacheItem
)
return
nil
...
...
@@ -46,9 +46,9 @@ func (s *memcacheStorage) Get(key string) (interface{}, error) {
return
nil
,
err
}
item
:=
&
Item
{}
item
:=
&
cached
Item
{}
err
=
D
ecodeGob
(
i
.
Value
,
item
)
err
=
d
ecodeGob
(
i
.
Value
,
item
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
pkg/infra/distcache/redis_storage.go
View file @
c8ff698d
...
...
@@ -22,8 +22,8 @@ func newRedisStorage(c *redis.Client) *redisStorage {
// Set sets value to given key in session.
func
(
s
*
redisStorage
)
Put
(
key
string
,
val
interface
{},
expires
time
.
Duration
)
error
{
item
:=
&
Item
{
Created
:
getTime
()
.
Unix
(),
Val
:
val
}
value
,
err
:=
E
ncodeGob
(
item
)
item
:=
&
cachedItem
{
Val
:
val
}
value
,
err
:=
e
ncodeGob
(
item
)
if
err
!=
nil
{
return
err
}
...
...
@@ -42,8 +42,8 @@ func (s *redisStorage) Put(key string, val interface{}, expires time.Duration) e
func
(
s
*
redisStorage
)
Get
(
key
string
)
(
interface
{},
error
)
{
v
:=
s
.
c
.
Get
(
key
)
item
:=
&
Item
{}
err
:=
D
ecodeGob
([]
byte
(
v
.
Val
()),
item
)
item
:=
&
cached
Item
{}
err
:=
d
ecodeGob
([]
byte
(
v
.
Val
()),
item
)
if
err
==
nil
{
return
item
.
Val
,
nil
...
...
@@ -65,16 +65,3 @@ func (s *redisStorage) Delete(key string) error {
cmd
:=
s
.
c
.
Del
(
key
)
return
cmd
.
Err
()
}
// RedisProvider represents a redis session provider implementation.
type
RedisProvider
struct
{
c
*
redis
.
Client
duration
time
.
Duration
prefix
string
}
// Exist returns true if session with given ID exists.
func
(
p
*
RedisProvider
)
Exist
(
sid
string
)
bool
{
has
,
err
:=
p
.
c
.
Exists
(
p
.
prefix
+
sid
)
.
Result
()
return
err
==
nil
&&
has
}
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