rediscacheadapters

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

GitHub go.mod Go version go.dev reference Go Report Card GitHub Twitter Follow

Cache Adapter implementation for Redis

A CacheAdapter implementation that allows to connect and use a Redis instance.

Beware that at the moment it has the github.com/gomodule/redigo dependency

Usage

Please refer to the following example for the correct usage:

package main

import (
	"log"
	"time"

	"github.com/gomodule/redigo/redis"
	cacheadapters "github.com/tryvium-travels/golang-cache-adapters"
	rediscacheadapters "github.com/tryvium-travels/golang-cache-adapters/redis"
)

func main() {
	redisURI := "rediss://my-redis-instance-uri:port"

	myRedisPool := &redis.Pool{
		Dial: func() (redis.Conn, error) {
			// obtain a redis connection, there
			// are plenty of ways to do that.
			return redis.DialURL(redisURI)
		},
	}

	exampleTTL := time.Hour

	adapter, err := rediscacheadapters.New(myRedisPool, exampleTTL)
	if err != nil {
		// remember to check for errors
		log.Fatalf("Adapter initialization error: %s", err)
	}

	type exampleStruct struct {
		Value string
	}

	exampleKey := "a:redis:key"

	var exampleValue exampleStruct
	err = adapter.Get(exampleKey, &exampleValue)
	if err != nil {
		// remember to check for errors
		log.Fatalf("adapter.Get error: %s", err)
	}

	exampleKey = "another:redis:key"

	// nil TTL represents the default value put in the New function
	err = adapter.Set(exampleKey, exampleValue, nil)
	if err != nil {
		// remember to check for errors
		log.Fatalf("adapter.Get error: %s", err)
	}
}

Documentation

Overview

Package rediscacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for Redis clusters, along with some helper methods to create instances.

More info on Redis at https://redis.io

Index

Constants

This section is empty.

Variables

View Source
var (
	//ErrInvalidConnection will come out if you try to use an invalid connection in a session.
	ErrInvalidConnection = fmt.Errorf("cannot use an invalid connection")
)

Functions

func New

func New(pool *redis.Pool, defaultTTL time.Duration) (cacheadapters.CacheAdapter, error)

New creates a new RedisAdapter from an initialized Redis pool.

func NewSession

func NewSession(conn redis.Conn, defaultTTL time.Duration) (cacheadapters.CacheSessionAdapter, error)

NewSession creates a new Redis Cache Session adapter from an existing Redis connection.

Types

type RedisAdapter

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

RedisAdapter is the CacheAdapter implementation for Redis.

func (*RedisAdapter) Delete added in v0.0.2

func (ra *RedisAdapter) Delete(key string) error

Delete deletes a key from the cache.

func (*RedisAdapter) Get

func (ra *RedisAdapter) Get(key string, objectRef interface{}) error

Get obtains a value from the cache using a key, then tries to unmarshal it into the object reference passed as parameter.

func (*RedisAdapter) OpenSession

func (ra *RedisAdapter) OpenSession() (cacheadapters.CacheSessionAdapter, error)

OpenSession opens a new Cache Session.

func (*RedisAdapter) Set

func (ra *RedisAdapter) Set(key string, object interface{}, TTL *time.Duration) error

Set sets a value represented by the object parameter into the cache, with the specified key.

func (*RedisAdapter) SetTTL added in v0.0.2

func (ra *RedisAdapter) SetTTL(key string, newTTL time.Duration) error

SetTTL marks the specified key new expiration, deletes it via using cacheadapters.TTLExpired or negative duration.

type RedisCommandFunc added in v0.0.5

type RedisCommandFunc func(commandName string, args ...interface{})

type RedisSessionAdapter

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

RedisSessionAdapter is the CacheSessionAdapter implementation for Redis.

func (*RedisSessionAdapter) Close

func (rsa *RedisSessionAdapter) Close() error

Close closes the Cache Session.

func (*RedisSessionAdapter) Delete added in v0.0.2

func (rsa *RedisSessionAdapter) Delete(key string) error

Delete deletes a key from the cache.

func (*RedisSessionAdapter) Get

func (rsa *RedisSessionAdapter) Get(key string, objectRef interface{}) error

Get obtains a value from the cache using a key, then tries to unmarshal it into the object reference passed as parameter.

func (*RedisSessionAdapter) Set

func (rsa *RedisSessionAdapter) Set(key string, object interface{}, TTL *time.Duration) error

Set sets a value represented by the object parameter into the cache, with the specified key.

func (*RedisSessionAdapter) SetTTL added in v0.0.2

func (rsa *RedisSessionAdapter) SetTTL(key string, newTTL time.Duration) error

SetTTL marks the specified key new expiration, deletes it via using cacheadapters.TTLExpired or negative duration.

Jump to

Keyboard shortcuts

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