cache

package
Version: v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2018 License: MIT Imports: 7 Imported by: 8

Documentation

Overview

Package cache implements RDAP Service Registry file caching.

There are two separate implementations: MemoryCache and DiskCache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DiskCache

type DiskCache struct {
	// Duration files are stored before they're considered expired.
	//
	// The default is 24 hours.
	Timeout time.Duration

	// Directory to store cached files in.
	//
	// The default is $HOME/.openrdap.
	Dir string
	// contains filtered or unexported fields
}

A DiskCache caches Service Registry files on disk.

By default they're saved as $HOME/.openrdap/{asn,dns,ipv4,ipv6}.json. File mtimes are used to calculate cache expiry.

The cache directory is created automatically as needed.

func NewDiskCache

func NewDiskCache() *DiskCache

NewDiskCache creates a new DiskCache.

func (*DiskCache) InitDir

func (d *DiskCache) InitDir() (bool, error)

InitDir creates the cache directory if it does not already exist.

Returns true if the directory was created, or false if it already exists/or on error.

func (*DiskCache) Load

func (d *DiskCache) Load(filename string) ([]byte, error)

Load loads the file |filename| from disk.

Since Service Registry files do not change much, the file is returned even if its State() is Expired.

An error is returned if the file is not on disk.

func (*DiskCache) Save

func (d *DiskCache) Save(filename string, data []byte) error

Save saves the file |filename| with |data| to disk.

The cache directory is created if necessary.

func (*DiskCache) SetTimeout

func (d *DiskCache) SetTimeout(timeout time.Duration)

SetTimeout sets the duration each Service Registry file can be stored before its State() is Expired.

func (*DiskCache) State

func (d *DiskCache) State(filename string) FileState

State returns the cache state of the file |filename|.

The returned state is one of: Absent, Good, ShouldReload, Expired.

type FileState

type FileState int
const (
	// File is not in the cache.
	Absent FileState = iota

	// File is in the cache. The latest version has already accessed (Load or Saved()).
	Good

	// File is in the cache. A newer version of is available to be Load()'ed.
	//
	// This is used by DiskCache, which uses a shared cache directory.
	ShouldReload

	// File is in the cache, but has expired. It still can be Load()'ed.
	Expired
)

func (FileState) String

func (f FileState) String() string

type MemoryCache

type MemoryCache struct {
	Timeout time.Duration
	// contains filtered or unexported fields
}

A MemoryCache caches Service Registry files in memory.

func NewMemoryCache

func NewMemoryCache() *MemoryCache

NewMemoryCache creates a new MemoryCache.

func (*MemoryCache) Load

func (m *MemoryCache) Load(filename string) ([]byte, error)

Load returns the file |filename| from the cache.

Since Service Registry files do not change much, the file is returned even if its State() is Expired.

An error is returned if the file is not in the cache.

func (*MemoryCache) Save

func (m *MemoryCache) Save(filename string, data []byte) error

Save saves the file |filename| with |data| to the cache.

func (*MemoryCache) SetTimeout

func (m *MemoryCache) SetTimeout(timeout time.Duration)

SetTimeout sets the duration each Service Registry file can be stored before its State() is Expired.

func (*MemoryCache) State

func (m *MemoryCache) State(filename string) FileState

State returns the cache state of the file |filename|.

The returned state is one of: Absent, Good, Expired.

type RegistryCache

type RegistryCache interface {
	Load(filename string) ([]byte, error)
	Save(filename string, data []byte) error

	State(filename string) FileState

	SetTimeout(timeout time.Duration)
}

A RegistryCache implements a cache of Service Registry files.

Jump to

Keyboard shortcuts

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