2017-12-28 18:14:33 +01:00
|
|
|
package redisdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2018-03-09 17:47:38 +01:00
|
|
|
"github.com/lbryio/lbry.go/errors"
|
|
|
|
|
2017-12-28 18:14:33 +01:00
|
|
|
"github.com/garyburd/redigo/redis"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
redisHashKey = "ytsync"
|
|
|
|
redisSyncedVal = "t"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DB struct {
|
|
|
|
pool *redis.Pool
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() *DB {
|
|
|
|
var r DB
|
|
|
|
r.pool = &redis.Pool{
|
|
|
|
MaxIdle: 3,
|
|
|
|
IdleTimeout: 5 * time.Minute,
|
|
|
|
Dial: func() (redis.Conn, error) { return redis.Dial("tcp", ":6379") },
|
|
|
|
TestOnBorrow: func(c redis.Conn, t time.Time) error {
|
|
|
|
if time.Since(t) < time.Minute {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
_, err := c.Do("PING")
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r DB) IsPublished(id string) (bool, error) {
|
|
|
|
conn := r.pool.Get()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
alreadyPublished, err := redis.String(conn.Do("HGET", redisHashKey, id))
|
|
|
|
if err != nil && err != redis.ErrNil {
|
2018-03-09 17:47:38 +01:00
|
|
|
return false, errors.Prefix("redis error", err)
|
2017-12-28 18:14:33 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if alreadyPublished == redisSyncedVal {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r DB) SetPublished(id string) error {
|
|
|
|
conn := r.pool.Get()
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
_, err := redis.Bool(conn.Do("HSET", redisHashKey, id, redisSyncedVal))
|
|
|
|
if err != nil {
|
2018-03-09 17:47:38 +01:00
|
|
|
return errors.Prefix("redis error", err)
|
2017-12-28 18:14:33 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|