redis

package
v0.0.0-...-8e84a60 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package redis provides an io/fs.FS implementation that can be used in our cache.FS package.

Here's an example that simply accesses a local Redis instance:

redisFS, err := redis.New(
	redis.Args{Addr: "127.0.0.1:6379"},
	// This causes all files to exire in 5 minutes.
	// You can write a complex ruleset to handle different files at
	// different rates.
	redis.WithWriteFileOFOptions(
		regexp.MustCompile(`.*`),
		redis.ExpireFiles(5 * time.Minute),
	),
)
if err != nil {
	// Do something
}

if err := redisFS.WriteFile("gopher.jpg", gopherBytes, 0644); err != nil {
	// Do something
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExpireFiles

func ExpireFiles(d time.Duration) jsfs.OFOption

ExpireFiles expires files at duration d. If not set for a file, redis.KeepTTL is used.

func Flags

func Flags(flags int) jsfs.OFOption

Flags allows the passing of os.O_RDONLY/os.O_WRONLY/O_EXCL/O_TRUNC/O_CREATE flags to OpenFile(). By default this is O_RDONLY.

Types

type Args

type Args = redis.Options

Args is arguments to the Redis client.

type FS

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

FS provides an io.FS implementation using Redis.

func New

func New(args Args, options ...Option) (*FS, error)

New is the constructor for FS that implements fs.OpenFile and io.FS using Redis.

func (*FS) Open

func (f *FS) Open(name string) (fs.File, error)

Open implements fs.FS.Open().

func (*FS) OpenFile

func (f *FS) OpenFile(name string, mode fs.FileMode, options ...jsfs.OFOption) (fs.File, error)

OpenFile implements fs.OpenFiler.OpenFile(). We support os.O_CREATE, os.O_EXCL, os.O_RDONLY, os.O_WRONLY, and os.O_TRUNC. If OpenFile is passed O_RDONLY, this calls Open() and ignores all options. When writing a file, the file is not written until Close() is called on the file.

func (*FS) ReadFile

func (f *FS) ReadFile(name string) ([]byte, error)

ReadFile implements fs.ReadFileFS.ReadFile().

func (*FS) Remove

func (f *FS) Remove(name string) error

Remove attempts to remove file at name from FS.

func (*FS) Stat

func (f *FS) Stat(name string) (fs.FileInfo, error)

Stat implements fs.StatFS.Stat(). The FileInfo returned name and size can be used, but the others are static values. ModTime will always be the zero value. It should be noted that this is simple a bad wrapper on Open(), so the content is read as I did not see a way to query Redis for just the key size (and to be honest, I didn't dig to hard).

func (*FS) WriteFile

func (f *FS) WriteFile(name string, content []byte, perm fs.FileMode) error

WriteFile writes a file to name with content. This will overrite an existing entry. Passed perm must be 0644.

type Option

type Option func(f *FS) error

Option is an optional argument for the New() constructor.

func WithWriteFileOFOptions

func WithWriteFileOFOptions(regex *regexp.Regexp, options ...jsfs.OFOption) Option

WithWriteFileOFOption uses a regex on the file path given and if it matches will apply the options provided on that file when .WriteFile() is called. First match wins. A "nil" for a regex applies to all that are not matched. It is suggested for speed reasons to keep this relatively small or the first rules should match the majority of files. This can be passed multiple times with different regexes.

Jump to

Keyboard shortcuts

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