Skip to content

tinh-tinh/cacher

Repository files navigation

Cache for Tinh Tinh

Tinh Tinh Logo

Overview

The Cache Manager provides a unified API to manage caching in Tinh Tinh applications. It supports memory, Memcache, and Redis backends, and can be flexibly configured and injected into your modules and controllers.

Features

  • 🔌 Pluggable Backends: Supports in-memory, Memcache, and Redis stores
  • 🛡️ Type Safety: Generic interface for strong typing
  • 🏷️ Namespace Support: Isolate cache by logical namespace
  • 📦 Compression: Optional data compression
  • 🌐 Context-aware: Supports context propagation for advanced use cases
  • 🎣 Hooks: Register hooks for cache lifecycle events

Installation

go get -u github.com/tinh-tinh/cacher/v2

Basic Usage

Setting Up an In-Memory Cache

import "github.com/tinh-tinh/cacher/v2"

cache := cacher.NewSchema[string](cacher.Config{
    Store: cacher.NewInMemory(cacher.StoreOptions{
        Ttl: 15 * time.Minute,
    }),
})

err := cache.Set("users", "John")
data, err := cache.Get("users")

Using Namespaces

cache1 := cacher.NewSchema[string](cacher.Config{
    Store:     store,
    Namespace: "cache1",
})
cache2 := cacher.NewSchema[string](cacher.Config{
    Store:     store,
    Namespace: "cache2",
})

Memcache Example

import "github.com/tinh-tinh/cacher/storage/memcache"

cache := memcache.New(memcache.Options{
    Addr: []string{"localhost:11211"},
    Ttl:  15 * time.Minute,
})

Redis Example

import (
    "github.com/tinh-tinh/cacher/storage/redis"
    redis_store "github.com/redis/go-redis/v9"
)

cache := redis.New(redis.Options{
    Connect: &redis_store.Options{
        Addr: "localhost:6379",
    },
    Ttl: 15 * time.Minute,
})

API Overview

The main cache interface provides:

  • Set(key, value, opts...): Store a value
  • Get(key): Retrieve a value
  • Delete(key): Remove a value
  • Clear(): Remove all values
  • MSet(...params): Batch set
  • MGet(...keys): Batch get

Module Integration

You can register the cache as a provider in a Tinh Tinh module and inject it into controllers:

import (
    "github.com/tinh-tinh/cacher/v2"
    "github.com/tinh-tinh/tinhtinh/v2/core"
)

func userController(module core.Module) core.Controller {
    cache := cacher.Inject[[]byte](module)
    ctrl := module.NewController("users")

    ctrl.Get("", func(ctx core.Ctx) error {
        data, err := cache.Get("users")
        // handle data
    })
    return ctrl
}

To register the cache provider:

module := core.NewModule(core.NewModuleOptions{
    Imports: []core.Modules{
        cacher.Register(cacher.Config{ Store: cache }),
        userModule,
    },
})

Advanced Features

Compression

Set CompressAlg in Config to enable data compression:

cache := cacher.NewSchema[string](cacher.Config{
    Store: store,
    CompressAlg: "gzip", // Enable compression
})

Hooks

Use the Hooks field to register cache lifecycle hooks:

cache := cacher.NewSchema[string](cacher.Config{
    Store: store,
    Hooks: []cacher.Hook{
        // Add your hooks here
    },
})

Context Operations

Use SetCtx and GetCtx for context-aware operations:

ctx := context.Background()
err := cache.SetCtx(ctx, "key", value)
data, err := cache.GetCtx(ctx, "key")

Testing

The repository includes comprehensive tests for all stores and features. See:

  • cacher_test.go
  • storage/memcache/memcache_test.go

Contributing

We welcome contributions! Please feel free to submit a Pull Request.

Support

If you encounter any issues or need help, you can:

  • Open an issue in the GitHub repository
  • Check our documentation
  • Join our community discussions

About

⚡Cache Manager for Tinh Tinh framework

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •