cache

package
v0.0.0-...-dd4e4d9 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cache implements DNS response cache.

Index

Examples

Constants

View Source
const URLEnvironmentVariable = "REDIS_URL"

URLEnvironmentVariable is the name of the environment variable containing the Redis URL.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a DNS response cache.

func OpenCache

func OpenCache(backend CacheBackend) (*Cache, error)

OpenCache opens the cache.

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, domain string, requestType dnsmessage.Type) []byte

Get returns a cached DNS response, or nil.

Example
cache, err := OpenCache(&MemoryBackend{})
if err != nil {
	panic(err)
}

cache.Set(context.Background(), "wikipedia.org", dnsmessage.TypeA, []byte{1, 2, 3, 4}, 3600)

fmt.Print(cache.Get(context.Background(), "wikipedia.org", dnsmessage.TypeA))
Output:

[1 2 3 4]

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, domain string, requestType dnsmessage.Type, response []byte, expiry int)

Set adds a DNS response in the cache, or replaces a cache entry, while optionally settings the cache entry's expiry time (specified in seconds, 0 means no expiry).

type CacheBackend

type CacheBackend interface {
	Connect() error
	WithContext(context.Context) CacheBackend
	Set(string, []byte, int)
	Get(string) []byte
}

type MemoryBackend

type MemoryBackend struct {
	CacheBackend
	// contains filtered or unexported fields
}

MemoryBackend is an in-memory, freecache-based caching backend.

func (*MemoryBackend) Connect

func (mb *MemoryBackend) Connect() error

func (*MemoryBackend) Get

func (mb *MemoryBackend) Get(key string) []byte

func (*MemoryBackend) Set

func (mb *MemoryBackend) Set(key string, value []byte, expiry int)

func (*MemoryBackend) WithContext

func (mb *MemoryBackend) WithContext(_ context.Context) CacheBackend

type RedisBackend

type RedisBackend struct {
	CacheBackend
	// contains filtered or unexported fields
}

RedisBackend is a Redis-based caching backend.

func (*RedisBackend) Connect

func (rb *RedisBackend) Connect() error

func (*RedisBackend) Get

func (rb *RedisBackend) Get(key string) []byte

func (*RedisBackend) Set

func (rb *RedisBackend) Set(key string, value []byte, expiry int)

func (*RedisBackend) WithContext

func (rb *RedisBackend) WithContext(ctx context.Context) CacheBackend

Jump to

Keyboard shortcuts

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