GolangNote

Golang笔记

简单的go应用层缓存

Permalink

适用场合:保存全局设置等内容

使用例子:

Go: 缓存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main

import (
	"fmt"

	"github.com/ego008/hicache"
)

type setting struct {
	Title string
	Count int
	URL   string
}

func main() {
	c := hicache.New()

	c.Set("a", 222)
	v, ok := c.Get("a")
	fmt.Println(v, ok) // 222 true

	c.Del("a")
	fmt.Println(c.Get("a")) // <nil> false

	fmt.Println(c.Incr("b", 9))  // 9
	fmt.Println(c.Incr("b", 1))  // 10
	fmt.Println(c.Incr("b", -3)) // 7

	c.Set("c", "cvalue")
	fmt.Println(c.Incr("c", 1)) // 1

	st := setting{"test", 101, "http://www.google.com"}
	c.Set("d", st)
	v, ok = c.Get("d")
	if ok {
		fmt.Println(v)
		s := v.(setting)     // {test 101 http://www.google.com}
		fmt.Println(s.Title) // test
		fmt.Println(s.URL)   // http://www.google.com
	}

	fmt.Println(c.Count()) // 3

	c.Flush()
	fmt.Println(c.Count()) // 0
}

项目地址 https://github.com/ego008/hicache

本文网址: https://golangnote.com/topic/129.html 转摘请注明来源

Related articles

Write a Comment to "简单的go应用层缓存"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms