redis

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 11 Imported by: 0

README

Redis

A redis disk driver for facades.Cache() of Goravel.

Version

goravel/redis goravel/framework
v1.2.* v1.14.*
v1.1.* v1.13.*
v1.0.* v1.12.*

Install

  1. Add package
go get -u github.com/goravel/redis
  1. Register service provider
// config/app.go
import "github.com/goravel/redis"

"providers": []foundation.ServiceProvider{
    ...
    // Need register redis service provider before cache service provider
    &redis.ServiceProvider{},
   // Exists in the config/app.go file, DO NOT copy this line
    &cache.ServiceProvider{},
    ...
}
  1. Add your redis configuration to config/cache.go file
import (
    "github.com/goravel/framework/contracts/cache"
    redisfacades "github.com/goravel/redis/facades"
)

"stores": map[string]any{
    ...
    "redis": map[string]any{
        "driver": "custom",
        "connection": "default",
        "via": func() (cache.Driver, error) {
            return redisfacades.Redis("redis"), nil // The `redis` value is the key of `stores`
        },
    },
},
  1. Fill redis configuration to config/database.go file
// config/database.go
"redis": map[string]any{
    "default": map[string]any{
        "host":     config.Env("REDIS_HOST", ""),
        "password": config.Env("REDIS_PASSWORD", ""),
        "port":     config.Env("REDIS_PORT", 6379),
        "database": config.Env("REDIS_DB", 0),
    },
},

To enable TLS/SSL, you need to add the following configuration:

import "crypto/tls"

// config/database.go
"redis": map[string]any{
    "default": map[string]any{
        "host":     config.Env("REDIS_HOST", ""),
        "password": config.Env("REDIS_PASSWORD", ""),
        "port":     config.Env("REDIS_PORT", 6379),
        "database": config.Env("REDIS_DB", 0),
        "tls":      &tls.Config{
            // Add your tls configuration here
        },
    },
},

Testing

Run command below to run test:

go test ./...

Documentation

Index

Constants

View Source
const Binding = "goravel.redis"
View Source
const NoExpiration time.Duration = 0

Variables

Functions

This section is empty.

Types

type Lock

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

func NewLock

func NewLock(instance contractscache.Driver, key string, t ...time.Duration) *Lock

func (*Lock) Block

func (r *Lock) Block(t time.Duration, callback ...func()) bool

func (*Lock) ForceRelease

func (r *Lock) ForceRelease() bool

func (*Lock) Get

func (r *Lock) Get(callback ...func()) bool

func (*Lock) Release

func (r *Lock) Release() bool

type Redis

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

func NewRedis

func NewRedis(ctx context.Context, config config.Config, store string) (*Redis, error)

func (*Redis) Add

func (r *Redis) Add(key string, value any, t time.Duration) bool

Add Driver an item in the cache if the key does not exist.

func (*Redis) Decrement

func (r *Redis) Decrement(key string, value ...int) (int, error)

func (*Redis) Flush

func (r *Redis) Flush() bool

Flush Remove all items from the cache.

func (*Redis) Forever

func (r *Redis) Forever(key string, value any) bool

Forever Driver an item in the cache indefinitely.

func (*Redis) Forget

func (r *Redis) Forget(key string) bool

Forget Remove an item from the cache.

func (*Redis) Get

func (r *Redis) Get(key string, def ...any) any

Get Retrieve an item from the cache by key.

func (*Redis) GetBool

func (r *Redis) GetBool(key string, def ...bool) bool

func (*Redis) GetInt

func (r *Redis) GetInt(key string, def ...int) int

func (*Redis) GetInt64

func (r *Redis) GetInt64(key string, def ...int64) int64

func (*Redis) GetString

func (r *Redis) GetString(key string, def ...string) string

func (*Redis) Has

func (r *Redis) Has(key string) bool

Has Check an item exists in the cache.

func (*Redis) Increment

func (r *Redis) Increment(key string, value ...int) (int, error)

func (*Redis) Lock

func (r *Redis) Lock(key string, t ...time.Duration) cache.Lock

func (*Redis) Pull

func (r *Redis) Pull(key string, def ...any) any

Pull Retrieve an item from the cache and delete it.

func (*Redis) Put

func (r *Redis) Put(key string, value any, t time.Duration) error

Put Driver an item in the cache for a given time.

func (*Redis) Remember

func (r *Redis) Remember(key string, seconds time.Duration, callback func() (any, error)) (any, error)

Remember Get an item from the cache, or execute the given Closure and store the result.

func (*Redis) RememberForever

func (r *Redis) RememberForever(key string, callback func() (any, error)) (any, error)

RememberForever Get an item from the cache, or execute the given Closure and store the result forever.

func (*Redis) WithContext

func (r *Redis) WithContext(ctx context.Context) cache.Driver

type ServiceProvider

type ServiceProvider struct {
}

func (*ServiceProvider) Boot

func (receiver *ServiceProvider) Boot(app foundation.Application)

func (*ServiceProvider) Register

func (receiver *ServiceProvider) Register(app foundation.Application)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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