当前位置:网站首页>Gcache of goframe memory cache

Gcache of goframe memory cache

2022-06-11 22:32:00 Wangzhongyang go

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 17 God , Click to see the event details

Basic concepts

gcache The module provides a high-speed memory cache by default , Very efficient operation ,CPU The performance loss is in ns Nanosecond level .

Basic use

package main

import (
   "fmt"
   "github.com/gogf/gf/os/gcache"
)

func main() {
   //  Create a cache object ,
   //  Of course, it can also be used directly and conveniently gcache Package method 
   c := gcache.New()

   //  Set the cache , Not overdue 
   c.Set("k1", "v1", 0)

   //  Access to the cache 
   v, _ := c.Get("k1")
   fmt.Println(v)

   //  Get cache size 
   n, _ := c.Size()
   fmt.Println(n)

   //  Whether the specified key name exists in the cache 
   b, _ := c.Contains("k1")
   fmt.Println(b)

   //  Delete and return the deleted key value 
   fmt.Println(c.Remove("k1"))

   //  Close the cache object , Give Way GC Recycle resources 
   c.Close()
}
 Copy code 

Print the results

image.png

Cache control

package main

import (
   "fmt"
   "github.com/gogf/gf/os/gcache"
   "time"
)

func main() {
   //  Write when the key name does not exist , Set expiration time 1000 millisecond 
   gcache.SetIfNotExist("k1", "v1", 1000*time.Millisecond)

   //  Print the current list of key names 
   keys, _ := gcache.Keys()
   fmt.Println(keys)

   //  Print the current key value list 
   values, _ := gcache.Values()
   fmt.Println(values)

   //  Get the specified key value , If it does not exist, write , And return the key value 
   v, _ := gcache.GetOrSet("k2", "v2", 0)
   fmt.Println(v)

   //  Print the current key value pair 
   data1, _ := gcache.Data()
   fmt.Println(data1)

   //  wait for 1 second , In order to k1:v1 Automatically expire 
   time.Sleep(time.Second)

   //  Print the current key value pair again , Find out k1:v1 It's overdue , only k2:v2
   data2, _ := gcache.Data()
   fmt.Println(data2)
}
 Copy code 

Print the results

image.png

Cache obsolescence control

package main

import (
   "fmt"
   "github.com/gogf/gf/os/gcache"
   "time"
)

func main() {
   //  Set up LRU Quantity eliminated 
   c := gcache.New(2)

   //  add to 10 Elements , Not overdue 
   for i := 0; i < 10; i++ {
      c.Set(i, i, 0)
   }
   n, _ := c.Size()
   fmt.Println(n)
   keys, _ := c.Keys()
   fmt.Println(keys)

   //  Read key name 1, Ensure that the key name is reserved first 
   v, _ := c.Get(1)
   fmt.Println(v)

   //  After waiting for a certain time ( Default 1 Check every second ),
   //  The elements will be eliminated from the old to the new 
   time.Sleep(2 * time.Second)
   n, _ = c.Size()
   fmt.Println(n)
   keys, _ = c.Keys()
   fmt.Println(keys)
}
 Copy code 

Print the results

image.png

Tips

GetOrSetFunc Use

GetOrSetFunc Get a cache value , Executes the specified when the cache does not exist f func() (interface{}, error), Cache this f Result value of method , And return the result .

summary

This article introduces gcache Basic use of . Stand alone applications can use gcache Do cache processing , Distributed applications can be used gredis Do cache processing , Next introduction gredis Use .

Last

Thank you for reading , Welcome to the third company : give the thumbs-up 、 Collection 、 Coin-operated ( Focus on )!!!

8e95dac1fd0b2b1ff51c08757667c47a.gif

原网站

版权声明
本文为[Wangzhongyang go]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112228545098.html