diff --git a/cache/cache.go b/cache/cache.go index d5bfc3f..33319b9 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -1,6 +1,7 @@ package cache import ( + "fmt" "runtime" "time" @@ -35,13 +36,27 @@ func initCache(host string, port int, username, password string, database int) * } // Set a key-value pair in the cache -func Set(key string, value []byte, exp time.Duration) error { - return store.Set(key, value, exp) +func Set[T any](key string, value *T, exp time.Duration) error { + return store.Set(key, helpers.ToBytes(value), exp) } // Get a key from the cache -func Get(key string) ([]byte, error) { - return store.Get(key) +func Get[T any](key string) (v *T, err error) { + val, err := store.Get(key) + if err != nil { + return v, err + } + + if len(val) == 0 { + return v, fmt.Errorf("key %s not found", key) + } + + v, err = helpers.FromBytes[*T](val) + if err != nil { + return v, err + } + + return v, nil } // Has a key in the cache diff --git a/gttp/http.go b/gttp/http.go index e1421b3..cafbe9d 100644 --- a/gttp/http.go +++ b/gttp/http.go @@ -7,6 +7,8 @@ import ( "io" "net/http" "time" + + "github.com/ochom/gutils/helpers" ) // Response is the response of the request. @@ -52,7 +54,7 @@ func getTimeout(timeout ...time.Duration) time.Duration { // Post sends a POST request to the specified URL. func Post(url string, headers M, body any, timeout ...time.Duration) (res *Response, err error) { - req, err := http.NewRequest("POST", url, bytes.NewBuffer(toBytes(body))) + req, err := http.NewRequest("POST", url, bytes.NewBuffer(helpers.ToBytes(body))) if err != nil { return } @@ -110,7 +112,7 @@ func Get(url string, headers M, timeout ...time.Duration) (res *Response, err er // Custom sends a custom request to the specified URL. func Custom(url, method string, headers M, body any, timeout ...time.Duration) (res *Response, err error) { - req, err := http.NewRequest(method, url, bytes.NewBuffer(toBytes(body))) + req, err := http.NewRequest(method, url, bytes.NewBuffer(helpers.ToBytes(body))) if err != nil { return } diff --git a/gttp/types.go b/gttp/types.go index c0014db..7fca690 100644 --- a/gttp/types.go +++ b/gttp/types.go @@ -1,34 +1,7 @@ package gttp -import "encoding/json" - // M is a map[string]string type M map[string]string // Map is a map[string]interface{} type Map map[string]interface{} - -// toBytes ... -func toBytes(payload any) []byte { - if payload == nil { - return nil - } - - // Check if the payload is already of type []byte. - if bytesPayload, ok := payload.([]byte); ok { - return bytesPayload - } - - // Check if the payload is already of type string. - if stringPayload, ok := payload.(string); ok { - return []byte(stringPayload) - } - - // Marshal the payload to JSON. - bytesPayload, err := json.Marshal(payload) - if err != nil { - return nil - } - - return bytesPayload -} diff --git a/helpers/bytes.go b/helpers/bytes.go new file mode 100644 index 0000000..976faf4 --- /dev/null +++ b/helpers/bytes.go @@ -0,0 +1,43 @@ +package helpers + +import "encoding/json" + +// toBytes ... +func ToBytes(payload any) []byte { + if payload == nil { + return nil + } + + // Check if the payload is already of type []byte. + if bytesPayload, ok := payload.([]byte); ok { + return bytesPayload + } + + // Check if the payload is already of type string. + if stringPayload, ok := payload.(*string); ok { + return []byte(*stringPayload) + } + + // Marshal the payload to JSON. + bytesPayload, err := json.Marshal(payload) + if err != nil { + return nil + } + + return bytesPayload +} + +// FromBytes ... +func FromBytes[T any](payload []byte) (T, error) { + var data T + if payload == nil { + return data, nil + } + + err := json.Unmarshal(payload, &data) + if err != nil { + return data, err + } + + return data, nil +}