caching

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2018 License: MPL-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package caching provides an easy to make a cache on top of the gc package used to track idle resources in taskcluster-worker.

Index

Constants

This section is empty.

Variables

View Source
var ErrDisposableSizeNotSupported = gc.ErrDisposableSizeNotSupported

ErrDisposableSizeNotSupported should be returned from DiskSize() and MemorySize() if said feature is not supported

Functions

This section is empty.

Types

type Cache

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

A Cache is an object that wraps a constructor and manages the life-cycle of objects. At creation a cache is given a Constructor, a ResourceTracker and the option of being an exclusive or shared-cache, meaning if resources may be re-used before they are released.

func New

func New(constructor Constructor, shared bool, tracker gc.ResourceTracker, monitor runtime.Monitor) *Cache

New returns a Cache wrapping constructor such that resources returned from Require are shared between all calls to Require with the same options, if shared is set to true. Otherwise, resources are exclusive.

func (*Cache) Purge

func (c *Cache) Purge(filter func(r Resource) bool) error

Purge cached resources, notice that these resources may be in use. The filter function should return true if the resource should be purged, then it'll purged when it is no-longer in use.

func (*Cache) PurgeAll

func (c *Cache) PurgeAll() error

PurgeAll will purge all resources returning the first error, then proceeding and purging everything else, before returning the first error, if any.

func (*Cache) Require

func (c *Cache) Require(ctx Context, options interface{}) (*Handle, error)

Require returns a resource for the given options

If the Cache is exclusive the resource will not be re-used before it is returned with a call to Release(resource). If the cache is shared the cache will only every create one instance of the resource, unless purged or freed by the ResourceTracker.

type Constructor

type Constructor func(ctx Context, options interface{}) (Resource, error)

A Constructor is function that given options creates a resource.

Notice, options must be JSON serializable, as they will be hashed to determine resource equivalence.

type Context

type Context interface {
	context.Context
	Progress(description string, percent float64)
}

Context for creation of resources

type Handle

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

Handle holds a reference to a Resource until Release() is called

func (*Handle) Release

func (h *Handle) Release()

Release releases the resource held by the Handle, it safe to call this repeatedly

func (*Handle) Resource

func (h *Handle) Resource() Resource

Resource returns the resource held by the Handle

type Resource

type Resource interface {
	MemorySize() (uint64, error)
	DiskSize() (uint64, error)
	Dispose() error
}

A Resource that can be cached must also be disposable

Jump to

Keyboard shortcuts

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