cacheadapters

package module
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: 2 Imported by: 0

README

GitHub go.mod Go version go.dev reference Go Report Card GitHub Twitter Follow Build and Test library

Golang Cache Adapters

A set of Cache Adapters for various distributed systems (like Redis) written in Go.

Now with a new MultiCacheAdapter implementation!

Supported CacheAdapter implementations

Library reference

Just check the GoDocs

Install

Just use the standard way

go get github.com/tryvium-travels/golang-cache-adapters

or use go modules, it should take care of this automagically.

Usage

The CacheAdapter interface offers 2 main ways to access a cache: Get and Set methods.

You can use Delete and SetTTL functions as well. For more info, check the docs.

This example creates a new RedisAdapter and uses it, but you can replace it with any of the other supported Adapters.

To know how to use the MultiCacheAdapter check out the docs.

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)
	}
}

Contributing

First of all, thank you!

To contribute to our project:

  1. Open an Issue signaling bugs or proposals using the provided templates
  2. If you are going to add a new adapter, please stick to the NAMING CONVENTION for branch, package, folder, file and adapter names.
  3. We require a total test coverage (100%) if we are going to accept a new adapter, we can help you if you have any difficulties to achieve that.
  4. Please be polite in issue and PR discussions and do not offend other people.

Naming Convention

The following naming convention applies if you want to contribute:

  1. Branch name should follow the following form

    adapters/{{ADAPTER_NAME}}-#{{GITHUB_ISSUE_ID}}

    Example branch name: adapters/redis-#1

  2. If you add a new adapter:

    The name of the folder must match the lowercase adapter name (e.g. redis/mongodb/memcached)

    We strongly suggest you to create an adapter and a session_adapter go file, although it is not required.

    Example file names:

    redis_adapter.go and redis_session_adapter.go

    Example test file names:

    redis_adapter_test.go and redis_session_adapter_test.go.

    There must also be test files with 100% test coverage.

Documentation

Overview

Package cacheadapters contains the generic entities used to access the cache.

In this package you will find the CacheAdapter and CacheSessionAdapter interfaces, used by the specific implementations to access the cache, along with a MultiCacheProvider struct you can use to access multiple cache adapters with an index-based priority mechanism.

If you want to see the specific implementations go to the folder with the
name of the implementation you are searching (e.g. "redis").

Index

Constants

View Source
const TTLExpired time.Duration = 0

TTLExpired represents the zero-value of a time expiration.

Variables

View Source
var (
	// ErrNotFound will come out if a key is not found in the cache.
	ErrNotFound = fmt.Errorf("the value tried to get has not been found, check if it may be expired")
	// ErrGetRequiresObjectReference will come out if a nil object
	// reference is passed in a Get operation.
	ErrGetRequiresObjectReference = fmt.Errorf("in Get operations it is mandatory to provide a non-nil object reference to store the result in, nil found")
	// ErrInvalidTTL will come out if you try to set a zero-or-negative
	// TTL in a Set operation.
	ErrInvalidTTL = fmt.Errorf("cannot provide a negative TTL to Set operations")
)

Functions

This section is empty.

Types

type CacheAdapter

type CacheAdapter interface {
	// OpenSession opens a new Cache Session.
	OpenSession() (CacheSessionAdapter, error)
	// contains filtered or unexported methods
}

CacheAdapter represents a Cache Mechanism abstraction.

type CacheSessionAdapter

type CacheSessionAdapter interface {
	// Close closes the Cache Session.
	Close() error
	// contains filtered or unexported methods
}

CacheSessionAdapter represents a Cache Session Mechanism abstraction.

Directories

Path Synopsis
Package inmemorycacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for in-memory cache, along with some helper methods to create instances.
Package inmemorycacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for in-memory cache, along with some helper methods to create instances.
Package mongodbcacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for a MongoDB instance based cache, along with some helper methods to create instances.
Package mongodbcacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for a MongoDB instance based cache, along with some helper methods to create instances.
Package multicacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter with the objective of accessing multiple cache sources with fallback support.
Package multicacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter with the objective of accessing multiple cache sources with fallback support.
Package rediscacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for Redis clusters, along with some helper methods to create instances.
Package rediscacheadapters contains the implementations of CacheAdapter and CacheSessionAdapter for Redis clusters, along with some helper methods to create instances.

Jump to

Keyboard shortcuts

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