cachego

package module
v0.16.1 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2020 License: MIT Imports: 1 Imported by: 27

README

Cachego

Build Status Codecov branch GoDoc Go Report Card License

Simple interface for caching

Installation

Cachego requires Go 1.13 or later.

go get github.com/faabiosr/cachego

Usage

package main

import (
	"log"
	"time"

	"github.com/faabiosr/cachego/sync"
)

func main() {
	cache := sync.New()

	if err := cache.Save("user_id", "1", 10*time.Second); err != nil {
		log.Fatal(err)
	}

	id, err := cache.Fetch("user_id")
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("user id: %s \n", id)

	keys := cache.FetchMulti([]string{"user_id", "user_name"})

	for k, v := range keys {
		log.Printf("%s: %s\n", k, v)
	}

	if cache.Contains("user_name") {
		cache.Delete("user_name")
	}

	if _, err := cache.Fetch("user_name"); err != nil {
		log.Printf("%v\n", err)
	}

	if err := cache.Flush(); err != nil {
		log.Fatal(err)
	}
}

Supported drivers

Documentation

Read the full documentation at https://pkg.go.dev/github.com/faabiosr/cachego.

Development

Requirements
Makefile
// Clean up
$ make clean

//Run tests and generates html coverage file
$ make cover

// Up the docker containers for testing
$ make docker

// Format all go files
$ make fmt

//Run linters
$ make lint

// Run tests
$ make test

License

This project is released under the MIT licence. See LICENSE for more details.

Documentation

Overview

Package cachego provides a simple way to use cache drivers.

Example Usage

The following is a simple example using memcached driver:

import (
  "fmt"
  "github.com/faabiosr/cachego"
  "github.com/bradfitz/gomemcache/memcache"
)

func main() {

  cache := cachego.NewMemcached(
      memcached.New("localhost:11211"),
  )

  cache.Save("foo", "bar")

  fmt.Println(cache.Fetch("foo"))
}

Index

Constants

View Source
const (
	// ErrCacheExpired returns an error when the cache key was expired.
	ErrCacheExpired = err("cache expired")

	// ErrFlush returns an error when flush fails.
	ErrFlush = err("unable to flush")

	// ErrSave returns an error when save fails.
	ErrSave = err("unable to save")

	// ErrDelete returns an error when deletion fails.
	ErrDelete = err("unable to delete")

	// ErrDecode returns an errors when decode fails.
	ErrDecode = err("unable to decode")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {

	// Contains check if a cached key exists
	Contains(key string) bool

	// Delete remove the cached key
	Delete(key string) error

	// Fetch retrieve the cached key value
	Fetch(key string) (string, error)

	// FetchMulti retrieve multiple cached keys value
	FetchMulti(keys []string) map[string]string

	// Flush remove all cached keys
	Flush() error

	// Save cache a value by key
	Save(key string, value string, lifeTime time.Duration) error
}

Cache is the top-level cache interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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