cache

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2025 License: MIT Imports: 6 Imported by: 0

README

Forge Cache Plugin

A Redis-based caching plugin for the Forge framework.

Features

  • Simple key-value caching with Redis
  • Support for TTL (Time To Live)
  • Atomic operations (Increment, Decrement)
  • Conditional operations (SetNX)
  • Cache prefixing for isolation
  • Automatic connection management

Installation

  1. Add the Redis dependency to your project:
go get github.com/redis/go-redis/v9
  1. Configure the plugin in your forge.yaml:
plugins:
  cache:
    host: localhost
    port: 6379
    password: ""  
    db: 0
    prefix: "forge:cache:"

Usage

import "github.com/goforgl/forge/plugins/cache"

// In your controller or service
func (c *YourController) SomeAction(ctx *forge.Context) error {
    // Get the cache plugin
    cachePlugin := c.App.GetPlugin("cache").(*cache.CachePlugin)
    
    // Set a value
    err := cachePlugin.Set(ctx.Context, "key", "value", time.Hour)
    if err != nil {
        return err
    }
    
    // Get a value
    var value string
    err = cachePlugin.Get(ctx.Context, "key", &value)
    if err != nil {
        return err
    }
    
    // Check if key exists
    exists, err := cachePlugin.Exists(ctx.Context, "key")
    if err != nil {
        return err
    }
    
    // Increment a counter
    count, err := cachePlugin.Increment(ctx.Context, "counter")
    if err != nil {
        return err
    }
    
    // Delete a key
    err = cachePlugin.Delete(ctx.Context, "key")
    if err != nil {
        return err
    }
    
    // Clear all cache
    err = cachePlugin.Clear(ctx.Context)
    if err != nil {
        return err
    }
    
    return nil
}

Available Methods

  • Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
  • Get(ctx context.Context, key string, value interface{}) error
  • Delete(ctx context.Context, key string) error
  • Clear(ctx context.Context) error
  • Exists(ctx context.Context, key string) (bool, error)
  • Increment(ctx context.Context, key string) (int64, error)
  • Decrement(ctx context.Context, key string) (int64, error)
  • SetNX(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error)
  • GetOrSet(ctx context.Context, key string, value interface{}, ttl time.Duration, fn func() (interface{}, error)) error

Configuration Options

Option Type Default Description
host string localhost Redis server host
port int 6379 Redis server port
password string "" Redis server password
db int 0 Redis database number
prefix string forge:cache: Prefix for all cache keys

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CachePlugin

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

func New

func New(app *forge.Application, config *Config) (*CachePlugin, error)

func (*CachePlugin) Clear

func (p *CachePlugin) Clear(ctx context.Context) error

func (*CachePlugin) Decrement

func (p *CachePlugin) Decrement(ctx context.Context, key string) (int64, error)

func (*CachePlugin) Delete

func (p *CachePlugin) Delete(ctx context.Context, key string) error

func (*CachePlugin) Exists

func (p *CachePlugin) Exists(ctx context.Context, key string) (bool, error)

func (*CachePlugin) Get

func (p *CachePlugin) Get(ctx context.Context, key string, value interface{}) error

func (*CachePlugin) GetOrSet

func (p *CachePlugin) GetOrSet(ctx context.Context, key string, value interface{}, ttl time.Duration, fn func() (interface{}, error)) error

func (*CachePlugin) Increment

func (p *CachePlugin) Increment(ctx context.Context, key string) (int64, error)

func (*CachePlugin) Set

func (p *CachePlugin) Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error

func (*CachePlugin) SetNX

func (p *CachePlugin) SetNX(ctx context.Context, key string, value interface{}, ttl time.Duration) (bool, error)

func (*CachePlugin) Shutdown

func (p *CachePlugin) Shutdown() error

type Config

type Config struct {
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	Password string `yaml:"password"`
	DB       int    `yaml:"db"`
	Prefix   string `yaml:"prefix"`
}

Jump to

Keyboard shortcuts

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