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
ttl

Jump to

Keyboard shortcuts

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