Skip to content
/ cache Public

🌵 Cache interface definition, easy, support horizontal expansion, key expiration and key tag

Notifications You must be signed in to change notification settings

cachego/cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cache

中文文档

Cache interface definition,provide a simple cache service based on memory,support horizontal expansion

Feature

  • easy to use

  • support horizontal expansion

  • support key expiration

  • support key tag

Related projects

Install

go module

Use go module directly import:

import "github.com/cachego/cache"

go get

Use go get to install:

go get github.com/cachego/cache

Demo

Complete example

1. cache demo

code reference:

package main

import (
	"fmt"

	"github.com/cachego/cache"
)

func main() {
	key := "key1"
	c := cache.NewInMemoryStrCache()
	c.Set(key, "cache value", 0)
	v, err := c.Get(key)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(v) // cache value
	c.Del(key)
	v, err = c.Get(key)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(v) // nil
}

output:


```text
cache value
<nil>

2. tag cache demo

code reference:

package main

import (
	"fmt"

	"github.com/cachego/cache"
)

func main() {
	key1 := "key1"
	key2 := "key2"
	tag := "tag1"
	c := cache.NewInMemoryStrTagCache()
	c.SetWithTag(key1, tag, "tag-cache value1", 0)
	c.SetWithTag(key2, tag, "tag-cache value2", 0)
	keys, _ := c.GetKeys(tag)
	fmt.Println(keys) // [key1 key2]
	v1, _ := c.Get(key1)
	fmt.Println(v1) // tag-cache value1
	v2, _ := c.Get(key2)
	fmt.Println(v2) // tag-cache value2

	c.DelWithTag(tag)
	keys, _ = c.GetKeys(tag)
	fmt.Println(keys) // []
	v11, _ := c.Get(key1)
	fmt.Println(v11) // nil
	v22, _ := c.Get(key2)
	fmt.Println(v22) // nil
}

output:

[key1 key2]
tag-cache value1
tag-cache value2
[]
<nil>
<nil>

About

🌵 Cache interface definition, easy, support horizontal expansion, key expiration and key tag

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages