gocache

package module
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2022 License: GPL-3.0 Imports: 6 Imported by: 0

README

gocache

Go library that provide abstract cache support.

The library currently supports two type of cache:

  • Redis.
  • In Memory.

Example usage

Using sugar Getxxx methods
package main

import (
	"fmt"
	"github.com/creekorful/gocache"
	"log"
	"time"
)

func main() {
	c := gocache.NewMemoryCache("test")

	totalUsers, err := c.GetInt64("totalUsers", func() (int64, time.Duration) {
		// Expensive operation to count number of users...
		totalUsers := int64(1000)
		return totalUsers, 5 * time.Minute
	})
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(totalUsers)
}
Using classic Get / Set
package main

import (
	"fmt"
	"github.com/creekorful/gocache"
	"log"
	"time"
)

func main() {
	c := gocache.NewMemoryCache("test")

	totalUsers, exists, err := c.Int64("totalUsers")
	if err != nil {
		log.Fatal(err)
	}

	if !exists {
		// Expensive operation to count number of users...
		totalUsers = int64(1000)

		if err := c.SetInt64("totalUsers", totalUsers, 5*time.Minute); err != nil {
			log.Fatal(err)
		}
	}

	fmt.Println(totalUsers)
}

Who's using the library?

Documentation

Index

Constants

View Source
const (
	NoExpiration time.Duration = 0
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Int64 retrieve the int64 value identified by given key
	// it returns the value, a boolean indicating if the value exists or not, and an error (if any)
	Int64(key string) (int64, bool, error)
	// SetInt64 set the int64 value identified by given key
	// it takes the key, the value to be set, and an optional TTL (see: NoExpiration)
	SetInt64(key string, value int64, ttl time.Duration) error
	// GetInt64 either retrieve the value identified by given key if it exists
	// otherwise it will run the callback, cache and return the value
	GetInt64(key string, callback func() (int64, time.Duration, error)) (int64, error)

	// Int retrieve the int value identified by given key
	// it returns the value, a boolean indicating if the value exists or not, and an error (if any)
	Int(key string) (int, bool, error)
	// SetInt set the int value identified by given key
	// it takes the key, the value to be set, and an optional TTL (see: NoExpiration)
	SetInt(key string, value int, ttl time.Duration) error
	// GetInt either retrieve the value identified by given key if it exists
	// otherwise it will run the callback, cache and return the value
	GetInt(key string, callback func() (int, time.Duration, error)) (int, error)

	// Time retrieve the time.Time value identified by given key
	// it returns the value, a boolean indicating if the value exists or not, and an error (if any)
	Time(key string) (time.Time, bool, error)
	// SetTime set the time.Time value identified by given key
	// it takes the key, the value to be set, and an optional TTL (see: NoExpiration)
	SetTime(key string, value time.Time, ttl time.Duration) error
	// GetTime either retrieve the value identified by given key if it exists
	// otherwise it will run the callback, cache and return the value
	GetTime(key string, callback func() (time.Time, time.Duration, error)) (time.Time, error)

	// Bytes retrieve the []byte value identified by given key
	// it returns the value, a boolean indicating if the value exists or not, and an error (if any)
	Bytes(key string) ([]byte, bool, error)
	// SetBytes set the []byte value identified by given key
	// it takes the key, the value to be set, and an optional TTL (see: NoExpiration)
	SetBytes(key string, value []byte, ttl time.Duration) error
	// GetBytes either retrieve the value identified by given key if it exists
	// otherwise it will run the callback, cache and return the value
	GetBytes(key string, callback func() ([]byte, time.Duration, error)) ([]byte, error)

	// Value retrieve the raw value identified by given key
	// it returns the value, a boolean indicating if the value exists or not, and an error (if any)
	Value(key string) (interface{}, bool, error)
	// SetValue set the raw value identified by given key
	// it takes the key, the value to be set, and an optional TTL (see: NoExpiration)
	SetValue(key string, value interface{}, ttl time.Duration) error
	// GetValue either retrieve the raw value identified by given key if it exists
	// otherwise it will run the callback, cache and return the value
	GetValue(key string, callback func() (interface{}, time.Duration, error)) (interface{}, error)

	// Delete the value identified by given key.
	// the function does not fail if key does not exist
	Delete(key string) error
}

Cache represent a cache

func NewMemoryCache

func NewMemoryCache(prefix string) Cache

NewMemoryCache return a Cache in-memory.

func NewRedisCache

func NewRedisCache(uri, password, prefix string) (Cache, error)

NewRedisCache return a Cache backed by a Redis instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL