-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/bosun: serving temporary configs from redis.
- Loading branch information
Craig Peterson
committed
Feb 12, 2016
1 parent
a70b059
commit 0fd695e
Showing
5 changed files
with
51 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package database | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/base64" | ||
|
||
"bosun.org/_third_party/github.com/garyburd/redigo/redis" | ||
|
||
"bosun.org/collect" | ||
"bosun.org/opentsdb" | ||
) | ||
|
||
type ConfigDataAccess interface { | ||
SaveTempConfig(text string) (hash string, err error) | ||
GetTempConfig(hash string) (text string, err error) | ||
} | ||
|
||
func (d *dataAccess) Configs() ConfigDataAccess { | ||
return d | ||
} | ||
|
||
const configLifetime = 60 * 24 * 14 // 2 weeks | ||
|
||
func (d *dataAccess) SaveTempConfig(text string) (string, error) { | ||
defer collect.StartTimer("redis", opentsdb.TagSet{"op": "SaveTempConfig"})() | ||
conn := d.GetConnection() | ||
defer conn.Close() | ||
|
||
sig := md5.Sum([]byte(text)) | ||
b64 := base64.StdEncoding.EncodeToString(sig[0:8]) | ||
_, err := conn.Do("SET", "tempConfig:"+b64, text, "EX", configLifetime) | ||
return b64, err | ||
} | ||
|
||
func (d *dataAccess) GetTempConfig(hash string) (string, error) { | ||
defer collect.StartTimer("redis", opentsdb.TagSet{"op": "GetTempConfig"})() | ||
conn := d.GetConnection() | ||
defer conn.Close() | ||
|
||
key := "tempConfig:" + hash | ||
dat, err := redis.String(conn.Do("GET", key)) | ||
if err != nil { | ||
return "", err | ||
} | ||
_, err = conn.Do("EXPIRE", key, configLifetime) | ||
return dat, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters