goFileCache

package module
v0.0.0-...-77af46e Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: GPL-3.0 Imports: 7 Imported by: 0

README

goFileCache

a file cache for golang use gob encode

Example

package main

import (
	"log"
	"time"

	fc "github.com/fy138/goFileCache"
)

type MyInfo struct {
	Name string
	Age  int
}
type School struct {
	SchoolName string
	Studen     struct {
		Name string
		Age  int
	}
}

func main() {
	fc := fc.NewFileCache("./tmp/")

	//key 1
	k1 := "myinfo"
	d := &MyInfo{Name: "fy", Age: 100}
	//store data to disk
	err := fc.SetCache(k1, d)
	if err != nil {
		log.Print(err)
	}
	//get data from cache
	mydata := &MyInfo{}
	//GetCache( key, expire time(seconds),data)
	err = fc.GetCache(k1, 7200, mydata)
	if err != nil {
		log.Print(err)
	}
	log.Printf("%s=>%d", mydata.Name, mydata.Age)

	//key2
	k2 := "schoolinfo"
	si := &School{SchoolName: "myschool"}
	si.Studen.Name = "test"
	si.Studen.Age = 10
	err = fc.SetCache(k2, si)
	if err != nil {
		log.Print(err)
	}
	scinfo := &School{}
	err = fc.GetCache(k2, 7200, scinfo)
	if err != nil {
		log.Print(err)
	}
	log.Printf("%s,%s=>%d", scinfo.SchoolName, si.Studen.Name, si.Studen.Age)
	//expired
	time.Sleep(time.Second * 5)
	err = fc.GetCache(k2, 4, scinfo)
	if err != nil {
		log.Print(err) //expired
	}
}	

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileCache

type FileCache struct {
	BaseDir string
}

func NewFileCache

func NewFileCache(basedir string) *FileCache

func (*FileCache) Delete

func (fc *FileCache) Delete(key string) error

func (*FileCache) GetCache

func (fc *FileCache) GetCache(key string, expire int, data interface{}) error

func (*FileCache) GetSavePath

func (fc *FileCache) GetSavePath(key string) string

func (*FileCache) SetCache

func (fc *FileCache) SetCache(key string, data interface{}) error

Jump to

Keyboard shortcuts

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