-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from catmullet/maintenance
Maintenance
- Loading branch information
Showing
7 changed files
with
68 additions
and
185 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
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,36 @@ | ||
package redisstore | ||
|
||
import ( | ||
"github.com/catmullet/one" | ||
"github.com/Kochava/collective2-ingestion-file-watcher/pkg/idempotency" | ||
"gopkg.in/redis.v5" | ||
"time" | ||
) | ||
|
||
// RedisOneStore struct to hold redis client and ttl | ||
type RedisOneStore struct { | ||
// RedisStore struct to hold redis client and ttl. | ||
type RedisStore struct { | ||
client *redis.Client | ||
ttl time.Duration | ||
} | ||
|
||
// NewRedisOneStore returns a new RedisOneStore with redis client using gopkg.in/redis.v5 | ||
func NewRedisOneStore(options *redis.Options, ttl time.Duration) *RedisOneStore { | ||
return &RedisOneStore{ | ||
// NewRedisStore returns a new RedisStore with redis client using https://gopkg.in/redis.v5. | ||
func NewRedisStore(options *redis.Options, ttl time.Duration) *RedisStore { | ||
return &RedisStore{ | ||
client: redis.NewClient(options), | ||
ttl: ttl, | ||
} | ||
} | ||
|
||
// AddKey adds key to redis if one does not exist otherwise returns error | ||
func (rds *RedisOneStore) AddKey(key string) (ok bool, err error) { | ||
isExisting, err := rds.HasKey(key) | ||
if isExisting || err == nil { | ||
return | ||
} | ||
|
||
_, err = rds.client.Set(key, nil, rds.ttl).Result() | ||
// AddKey adds key to redis if one does not exist otherwise returns error. | ||
func (rds *RedisStore) AddKey(key string) (bool, error) { | ||
incr, err := rds.client.Incr(key).Result() | ||
if err != nil { | ||
return | ||
return false, err | ||
} | ||
return true, nil | ||
} | ||
|
||
// HasKey returns error if key does not exist | ||
func (rds *RedisOneStore) HasKey(key string) (exists bool, err error) { | ||
_, err = rds.client.Get(key).Result() | ||
if err != nil { | ||
return false, one.ErrNoKeyExist | ||
if incr > 1 { | ||
return false, idempotency.ErrKeyExists() | ||
} | ||
return true, nil | ||
|
||
_, err = rds.client.Set(key, int64(1), rds.ttl).Result() | ||
|
||
return err == nil, err | ||
} |