fttl

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2024 License: MIT Imports: 9 Imported by: 0

README

fttl

Action Report Card Lines of code godoc License

xuender/fttl is a time to live cache based on file system.

🚀 Install

go install github.com/xuender/fttl@latest

💡 Usage

base
fdb := fttl.New(filepath.Join(os.TempDir(), "base"))
defer fdb.Close()

fdb.Put([]byte("key"), []byte("value"))

val, _ := fdb.Get([]byte("key"))
fmt.Println(string(val))
fmt.Println(fdb.Has([]byte("key")))

fdb.Delete([]byte("key"))
ttl
fdb.PutTTL([]byte("key"), []byte("value"), time.Minute, time.Second)

👤 Contributors

Contributors

📝 License

© ender, 2024~time.Now

MIT LICENSE

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrIsDir    = errors.New("is a directory")
	ErrNotFound = errors.New("not found")
)

Functions

func Hash

func Hash(key []byte) uint64
Example
package main

import (
	"fmt"

	"github.com/xuender/fttl"
)

func main() {
	fmt.Println(fttl.Hash([]byte("test")))

}
Output:
2771506328522617498

func Int2Bytes added in v0.0.3

func Int2Bytes[I constraints.Integer](num I) []byte

func IntHash added in v0.0.2

func IntHash[I constraints.Integer](num I) uint64
Example
package main

import (
	"fmt"

	"github.com/xuender/fttl"
)

func main() {
	fmt.Println(fttl.IntHash(1))
	fmt.Println(fttl.IntHash(2))
	fmt.Println(fttl.IntHash(3))

}
Output:
3139486886484431350
3238141947922148205
4470094458844362118

func Path

func Path(num uint64) (string, string)

Types

type DB

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

func New

func New(dir string) *DB

func (*DB) Close

func (p *DB) Close()

func (*DB) Delete

func (p *DB) Delete(key []byte) error

func (*DB) Get

func (p *DB) Get(key []byte) ([]byte, error)
Example
package main

import (
	"fmt"
	"os"

	"github.com/xuender/fttl"
)

func main() {
	fdb := fttl.New(os.TempDir())
	key := []byte("get")

	defer fdb.Delete(key)

	res := fdb.Put(key, []byte("value"))
	fmt.Println(res.Error)

	val, err := fdb.Get(key)
	fmt.Println(err)
	fmt.Println(string(val))

}
Output:
<nil>
<nil>
value

func (*DB) GetByHash added in v0.0.8

func (p *DB) GetByHash(hash uint64) ([]byte, error)

func (*DB) GetByPath added in v0.0.8

func (p *DB) GetByPath(path string) ([]byte, error)

func (*DB) Has added in v0.0.4

func (p *DB) Has(key []byte) bool
Example
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/xuender/fttl"
)

func main() {
	fdb := fttl.New(os.TempDir())
	key := []byte("has")

	defer fdb.Delete(key)

	fdb.PutTTL(key, []byte("value"), time.Millisecond*100, 0)

	fmt.Println(fdb.Has(key))
	time.Sleep(time.Millisecond * 200)

	fmt.Println(fdb.Has(key))

}
Output:
true
false

func (*DB) Put

func (p *DB) Put(key, value []byte) *Result
Example
package main

import (
	"fmt"
	"os"

	"github.com/xuender/fttl"
)

func main() {
	fdb := fttl.New(os.TempDir())
	key := []byte("put")

	defer fdb.Delete(key)
	fdb.Put(key, []byte("value"))
	fdb.Put(key, []byte("value2"))

	val, _ := fdb.Get(key)
	fmt.Println(string(val))

}
Output:
value2

func (*DB) PutTTL

func (p *DB) PutTTL(key, value []byte, expire, access time.Duration) *Result
Example
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/xuender/fttl"
)

func main() {
	fdb := fttl.New(os.TempDir())
	key := []byte("ttl")

	defer fdb.Delete(key)

	fdb.PutTTL(key, []byte("value"), time.Millisecond*100, 0)

	val, _ := fdb.Get(key)
	fmt.Println(string(val))
	time.Sleep(time.Millisecond * 200)

	_, err := fdb.Get(key)
	fmt.Println(err)

}
Output:
value
not found

func (*DB) Refresh added in v0.0.8

func (p *DB) Refresh(hash uint64) error

type Policy

type Policy struct {
	Expire time.Time     `json:"expire"`
	Access time.Duration `json:"access"`
}

type Result added in v0.0.8

type Result struct {
	Error error
	Hash  uint64
	Path  string
}

Directories

Path Synopsis
_example
base command
hash command
ttl command

Jump to

Keyboard shortcuts

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