cache

package module
v0.0.0-...-eb8797a Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2023 License: MIT Imports: 4 Imported by: 1

README

Cache - GO

A simple cache library for golang.

Installation

go get github.com/farzai/cache-go

Usage

package main

import (
	"fmt"
	"time"

	"github.com/farzai/cache-go"
)


func main() {
	cache := cache.NewCache(cache.NewRedisDriver("localhost:6379", "", 0, 10*time.Second))
	// Or
	// cache := cache.NewCache(cache.NewMemoryDriver(10*time.Second))
	// cache := cache.NewCache(cache.NewLocalFileDriver("/storage/cache", 10*time.Second))

	err := cache.Set("key", "value")
	if err != nil {
		fmt.Println("Error setting value in cache:", err)
		return
	}

	value, err := cache.Get("key")
	if err != nil {
		fmt.Println("Error getting value from cache:", err)
		return
	}

	fmt.Println("Value from cache:", value)
}

License

Please see the LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Set stores the given value in the cache with the specified expiration time.
	Set(key string, value interface{}, expiration time.Duration) error

	// Get retrieves the value from the cache with the specified key.
	Get(key string) (*CacheItem, error)

	// Delete removes the value from the cache with the specified key.
	Delete(key string) error

	// Flush removes all values from the cache.
	Flush() error

	// Has checks if the cache has the specified key.
	Has(key string) bool
}

Cache interface defines the methods for interacting with the cache.

type CacheItem

type CacheItem struct {
	Key        string        `json:"key"`
	Value      interface{}   `json:"value"`
	Expiration time.Duration `json:"expiration"`
	CreatedAt  time.Time     `json:"created_at"`
}

CacheItem represents a single cache item.

func (*CacheItem) ExpireAt

func (c *CacheItem) ExpireAt(t time.Time)

func (*CacheItem) ExpireIn

func (c *CacheItem) ExpireIn(duration time.Duration)

func (*CacheItem) Expired

func (c *CacheItem) Expired() bool

func (*CacheItem) Get

func (c *CacheItem) Get() interface{}

func (*CacheItem) Set

func (c *CacheItem) Set(value interface{})

type OptimizedRedisDriver

type OptimizedRedisDriver struct {
	// contains filtered or unexported fields
}

OptimizedRedisDriver implements the Cache interface using Redis as the storage driver with performance optimizations.

func NewOptimizedRedisDriver

func NewOptimizedRedisDriver(addr, password string, db int) *OptimizedRedisDriver

NewOptimizedRedisDriver creates a new OptimizedRedisDriver instance.

func NewOptimizedRedisDriverWithOptions

func NewOptimizedRedisDriverWithOptions(opt *redis.Options) *OptimizedRedisDriver

NewOptimizedRedisDriverWithOptions creates a new OptimizedRedisDriver instance with the specified options.

func (*OptimizedRedisDriver) Get

func (c *OptimizedRedisDriver) Get(key string, value interface{}) (bool, error)

Get retrieves the value from the cache for the given key. If the key is found, the value is returned from the cache. Returns true if the key is found in the cache, false otherwise.

func (*OptimizedRedisDriver) Set

func (c *OptimizedRedisDriver) Set(key string, value interface{}, expiration time.Duration) error

Set stores the given value in the cache with the specified expiration time.

type RedisDriver

type RedisDriver struct {
	// contains filtered or unexported fields
}

RedisDriver implements the Cache interface using Redis as the storage driver.

func NewRedisDriver

func NewRedisDriver(addr, password string, db int) *RedisDriver

NewRedisDriver creates a new RedisDriver instance.

func NewRedisDriverWithOptions

func NewRedisDriverWithOptions(opt *redis.Options) *RedisDriver

NewRedisDriverWithOptions creates a new RedisDriver instance with the specified options.

func (*RedisDriver) Get

func (c *RedisDriver) Get(key string, value interface{}) (bool, error)

Get retrieves the value from the cache for the given key. If the key is found, the value is unmarshaled into the provided variable. Returns true if the key is found in the cache, false otherwise.

func (*RedisDriver) Set

func (c *RedisDriver) Set(key string, value interface{}, expiration time.Duration) error

Set stores the given value in the cache with the specified expiration time.

Directories

Path Synopsis
local module

Jump to

Keyboard shortcuts

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