caching

package module
v2.0.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2015 License: Apache-2.0 Imports: 2 Imported by: 1

README

#Go-Caching

The go-caching packaging providers a scalable caching component for golang.

##Container

The container represents an adapter for caching manager/service.

###Available Containers

  • Concurrent Container: wrapping a container for concurrent access.

  • Multi-Level Container: wrapping the containers into a single container.

  • Memory Containers: local memory containers (not safe for concurrent access).

    • FIFO Container: replacement algorithm using FIFO (first in first out).

    • LFU Container: replacement algorithm using LFU (least frequently used).

    • LRU Container: replacement algorithm using LRU (least recently used).

    • MRU Container: replacement algorithm using MRU (most recently used).

    • ARC Container: replacement algorithm using ARC (adaptive/adjustable replacement cache).

  • Memcache Container: the adapter for memcached.

##Dependency

The dependency represents an external expiration policy.

###Available Dependencies

  • File Dependency: it's very useful for caching configuration file.

##Examples

###Example: File Configuration Caching

    import (
        "github.com/wayn3h0/go-caching"
        "github.com/wayn3h0/go-caching/container/memory"
        _ "github.com/wayn3h0/go-caching/container/memory/arc"
        "github.com/wayn3h0/go-caching/dependency/file"
    )

    container := memory.ARC.New(1000)                           // local memory container (ARC), capacity: 1000 (NOTE: not safe for concurrent access)
    cache := caching.NewCache(container)
    cache.Set("key", "value")                                   // memory container always returns nil error
    cache.Set("configuration-key", "settings", file.New(path))  // dependency by configuration file

###Example: Multi-Level Caching

    import (
        "github.com/wayn3h0/go-caching"
        "github.com/wayn3h0/go-caching/container/concurrent"
        "github.com/wayn3h0/go-caching/container/memcache"
        "github.com/wayn3h0/go-caching/container/memory"
        _ "github.com/wayn3h0/go-caching/container/memory/arc"
        "github.com/wayn3h0/go-caching/container/multilevel"
    )

    level1 := concurrent.New(memory.ARC.New(1000))              // local memory container (ARC), capacity: 1000, safe for concurrent access
    level2 := memcache.New("192.168.100.101:11211")
    container := multilevel.New(level1, level2)                 // local memory as level 1, memcached as level 2
    cache := caching.NewCache(container)
    err := cache.Set("key", "value")                            // memcached may return error
    if err != nil {
        // handle error
    }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache represents a cache.

func NewCache

func NewCache(container Container) *Cache

NewCache returns a new instance of Cache.

func (Cache) Clear

func (this Cache) Clear() error

Clear removes all caching items.

func (*Cache) Get

func (this *Cache) Get(key string) (interface{}, error)

Get returns the value of caching item. it returns nil if caching item has expired.

func (Cache) Remove

func (this Cache) Remove(key string) error

Remove removes the caching item.

func (Cache) Set

func (this Cache) Set(key string, value interface{}, expiration *Expiration, dependencies ...Dependency) error

Set sets the caching item.

type Container

type Container interface {
	// Get returns the value by given key.
	Get(key string) (interface{}, error)

	// Set inserts/updates the item.
	Set(key string, value interface{}) error

	// Remove removes the item by given key.
	Remove(key string) error

	// Clear removes all items.
	Clear() error
}

Container represents a container.

type Dependency

type Dependency interface {
	HasChanged() bool
}

Dependency represents an external expiration dependency.

type Expiration

type Expiration struct {
	AbsoluteTime  time.Time
	SlidingPeriod time.Duration
}

Expiration represents an expiration about time.

func NewExpiration

func NewExpiration(absoluteTime time.Time, slidingPeriod time.Duration) *Expiration

NewExpiration returns a new instance of Expiration.

type Item

type Item struct {
	Key              string
	Value            interface{}
	CreatedTime      time.Time
	LastAccessedTime time.Time
	Expiration       *Expiration
	Dependencies     []Dependency
}

Item represents an item.

func NewItem

func NewItem(key string, value interface{}, expiration *Expiration, dependencies ...Dependency) *Item

NewItem returns a new instance of Item.

func (Item) HasExpired

func (this Item) HasExpired() bool

HasExpired reports whether the item has expired.

Directories

Path Synopsis
container
dependency

Jump to

Keyboard shortcuts

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