Skip to content

Commit

Permalink
Not supporting old gob creater method
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Apr 26, 2020
1 parent 3da4915 commit c33a8c1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 57 deletions.
57 changes: 0 additions & 57 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ Package cache is a cache dependency management on-top of the famous redigo packa
package cache

import (
"fmt"
"time"

"github.com/gomodule/redigo/redis"
Expand Down Expand Up @@ -39,11 +38,6 @@ const (
setExpirationCommand string = "SETEX"
)

// GobCacheCreator can be used to generate the content to store. If the content
// is not found, the creator will be invoked and the result will be stored and
// returned
type GobCacheCreator func() ([]byte, error)

// Get gets a key from redis
func Get(key string) (string, error) {

Expand Down Expand Up @@ -394,57 +388,6 @@ func SetRemoveMember(set, member interface{}) (err error) {
return
}

// GetOrSetWithExpirationGob will return the cached value for the key or use the
// GobCacheCreator to create and insert the value into the cache. If the expiration
// time is set to a value greater than zero, the key will be set to expire in
// the provided duration
func GetOrSetWithExpirationGob(key string, fn GobCacheCreator, duration time.Duration, dependencies ...string) (data []byte, err error) {

// Create a new connection and defer closing
conn := GetConnection()
defer func() {
_ = conn.Close()
}()

// Get from redis
data, err = redis.Bytes(conn.Do(getCommand, key))

// Set the string in redis
if err != nil {

// Set the value
if data, err = fn(); err != nil {
return
}

// No data?!
if len(data) == 0 {
err = fmt.Errorf("value is empty for key: %s", key)
return
}

// Go routine to set the key and expiration
go func(key string, data []byte, duration time.Duration, dependencies []string) {

// Create a new connection and defer closing
/*connection := GetConnection()
defer func() {
_ = connection.Close()
}()*/

// Set an expiration time if found
if duration > 0 {
_ = SetExp(key, data, duration, dependencies...) // todo: handle the error?
} else {
_ = Set(key, data, dependencies...) // todo: handle the error?
}
}(key, data, duration, dependencies)
}

// Return the value
return
}

// DestroyCache will flush the entire redis server. It only removes keys, not scripts
func DestroyCache() (err error) {

Expand Down
11 changes: 11 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,14 @@ func TestGetList(t *testing.T) {
}
}
}

func TestGetOrSetWithExpirationGob(t *testing.T) {
// Create a local connection
if err := startTest(); err != nil {
t.Fatal(err.Error())
}

// Disconnect at end
defer endTest()

}

0 comments on commit c33a8c1

Please sign in to comment.