Added redis support
This commit is contained in:
parent
577664c034
commit
98a8040246
4 changed files with 101 additions and 1 deletions
39
caching/client.go
Normal file
39
caching/client.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package caching
|
||||
|
||||
import (
|
||||
"github.com/go-redis/redis"
|
||||
"time"
|
||||
)
|
||||
|
||||
type CacheClient struct {
|
||||
storage *redis.Client
|
||||
Expiration time.Duration
|
||||
}
|
||||
|
||||
var client *CacheClient
|
||||
|
||||
func GetClient() *CacheClient {
|
||||
if client != nil {
|
||||
return client
|
||||
}
|
||||
client = new(CacheClient)
|
||||
client.Init()
|
||||
return client
|
||||
}
|
||||
|
||||
func (client *CacheClient) Init() {
|
||||
client.storage = redis.NewClient(&redis.Options{
|
||||
Addr: HOST_NAME,
|
||||
Password: PASSWORD,
|
||||
DB: 0,
|
||||
})
|
||||
client.Expiration = CACHE_EXPIRATION
|
||||
}
|
||||
|
||||
func (client *CacheClient) Set(key string, value string) {
|
||||
client.storage.Set(key, value, client.Expiration)
|
||||
}
|
||||
|
||||
func (client *CacheClient) Get(key string) (string, error) {
|
||||
return client.storage.Get(key).Result()
|
||||
}
|
||||
9
caching/secrets.go
Normal file
9
caching/secrets.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package caching
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
const HOST_NAME = "localhost:6379"
|
||||
const PASSWORD = ""
|
||||
const CACHE_EXPIRATION = time.Hour * 24
|
||||
Loading…
Add table
Add a link
Reference in a new issue