filecache

package module
v0.0.0-...-52ce07f Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2014 License: MIT Imports: 2 Imported by: 6

README

go-filecache Build Status

This is a very small file cache library in Golang. It allows you to read a file from disk with a conditional update based on last modified time from os.Stat().

Updates are handled by passing in a function to perform the updates.

Example

The following example simply dumps a timestamp into a file. We will set the max age to 5 seconds, and run the example in a bash loop to demonstrate the cache timeout:

$ while sleep 1; do go run cachetest.go; done
2014-08-09 13:13:17.435246233 -0700 PDT
2014-08-09 13:13:17.435246233 -0700 PDT
2014-08-09 13:13:17.435246233 -0700 PDT
2014-08-09 13:13:17.435246233 -0700 PDT
2014-08-09 13:13:22.508095087 -0700 PDT
2014-08-09 13:13:22.508095087 -0700 PDT
2014-08-09 13:13:22.508095087 -0700 PDT
2014-08-09 13:13:22.508095087 -0700 PDT
2014-08-09 13:13:27.584300053 -0700 PDT

And the code:

package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"time"

	"github.com/ryanuber/go-filecache"
)

func main() {
	// Here we define a function which handles updating our file. In this case,
	// we just dump a timestamp into the file.
	updater := func(path string) error {
		f, err := os.Create(path)
		if err != nil {
			return err
		}
		defer f.Close()
		stamp := time.Now().String()
		_, err = f.Write([]byte(stamp))
		return err
	}

	// Create a new file cache, passing in the path to the file, the maximum
	// age, and the updater function.
	fc := filecache.New("testcache", 5*time.Second, updater)

	// Retrieve a file handle from the cache. The updater function is invoked
	// during this call if the max age is exceeded.
	fh, err := fc.Get()
	if err != nil {
		return
	}

	content, err := ioutil.ReadAll(fh)
	if err != nil {
		return
	}

	fmt.Println(string(content))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// The path to the file to cache
	Path string

	// The maximum elapsed time since the last file update.
	MaxAge time.Duration

	// The function used to handle updating the cached file.
	UpdateFunc Updater
}

Cache is a wrapper around os.File providing simple file caching.

func New

func New(path string, maxAge time.Duration, updater Updater) *Cache

New is a shortcut function for making a new file cache

func (*Cache) Expired

func (f *Cache) Expired() bool

Expired is a predicate which determines if the file should be updated

func (*Cache) Get

func (f *Cache) Get() (*os.File, error)

Get is used to retrieve an os.File handle. If the cache file has expired, this method will update it before opening it and returning the handle.

func (*Cache) Update

func (f *Cache) Update() error

Update calls the file update function (if present) on the cached file

type Updater

type Updater func(string) error

Updater is an interface for a function which will handle updating the file when it is expired.

Jump to

Keyboard shortcuts

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