Golang实践:用Sync.Map实现简易内存缓存系统

介绍

定义了一个Cache结构体,其中使用sync.Map作为底层数据结构来存储缓存项。Set方法用于设置缓存项,指定键、值以及过期时间。Get方法用于获取缓存项,如果缓存项存在且未过期,则返回值和true,否则返回nil和false。方法的接受者为指针类型,是为了对Cache对象进行操作,并在方法内部访问和修改对象的数据。
在main函数中,创建了一个Cache实例,并设置了两个缓存项。然后,我们尝试获取一个存在的缓存项,并在控制台上打印值。接着,我们等待6秒,再次尝试获取同一个缓存项,这次应该返回Key not found,因为该缓存项已过期。

代码

GoPratice/memcache/v0/simple.go at main · honlu/GoPratice

package main

import (
	"fmt"
	"sync"
	"time"
)

type Cache struct {
	data sync.Map
}

type cacheItem struct {
	value   interface{} // 值
	expired time.Time   // 过期时间
}

// Set 指针类型的方法接受者,才可以保证在方法内部访问和修改对象的数据。
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
	c.data.Store(key, cacheItem{
		value:   value,
		expired: time.Now().Add(duration),
	})
}

func (c *Cache) Get(key string) (interface{}, bool) {
	item, ok := c.data.Load(key)
	if !ok {
		return nil, false
	}

	cacheItem := item.(cacheItem)
	if time.Now().After(cacheItem.expired) { // 判断是否过期
		c.data.Delete(key)
		return nil, false
	}

	return cacheItem.value, true
}

func main() {
	cache := &Cache{}

	cache.Set("key1", "value1", 5*time.Second)
	cache.Set("key2", "value2", 10*time.Second)

	value1, found1 := cache.Get("key1")
	if found1 {
		fmt.Printf("Value1:%v\n", value1)
	} else {
		fmt.Printf("Key1 not found\n")
	}

	value2, found2 := cache.Get("key2")
	if found2 {
		fmt.Printf("Value2:%v\n", value2)
	} else {
		fmt.Printf("Key2 not found\n")
	}

	time.Sleep(6 * time.Second)

	value1, found1 = cache.Get("key1")
	if found1 {
		fmt.Printf("Value1:%v\n", value1)
	} else {
		fmt.Printf("Key1 not found\n")
	}
	value2, found2 = cache.Get("key2")
	if found2 {
		fmt.Printf("Value2:%v\n", value2)
	} else {
		fmt.Printf("Key2 not found\n")
	}
}

后续

可以考虑具体需求进行扩展和优化,例如添加缓存淘汰策略、高并发等。

备注:

内容首发在个人知乎文章,源码在Github

相关推荐

  1. Golang实践Sync.Map实现简易内存缓存系统

    2024-04-15 02:22:03       38 阅读
  2. golang实现一个定时任务

    2024-04-15 02:22:03       64 阅读
  3. Golang教程四(协程,channel,线程安全,syncMap

    2024-04-15 02:22:03       183 阅读
  4. linux文件系统的链接功能实现文件缓存LRU

    2024-04-15 02:22:03       54 阅读
  5. golang实现注册系统服务(Windows、Darwin)

    2024-04-15 02:22:03       57 阅读
  6. Golang实现简单队列(Queue)数据结构

    2024-04-15 02:22:03       52 阅读
  7. golang实现一个简单的HTTP server

    2024-04-15 02:22:03       57 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-15 02:22:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-15 02:22:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-15 02:22:03       82 阅读
  4. Python语言-面向对象

    2024-04-15 02:22:03       91 阅读

热门阅读

  1. Sql缺失索引查询,自动创建执行语句

    2024-04-15 02:22:03       38 阅读
  2. 【SpinalHDL】Scala编程中的class及case class

    2024-04-15 02:22:03       40 阅读
  3. vue 插槽使用

    2024-04-15 02:22:03       39 阅读
  4. HTML的基本结构

    2024-04-15 02:22:03       37 阅读
  5. MongoDB聚合运算符:$push

    2024-04-15 02:22:03       38 阅读
  6. 数据库-Redis(11)

    2024-04-15 02:22:03       28 阅读
  7. [蓝桥杯 2023 国 B] 班级活动

    2024-04-15 02:22:03       41 阅读
  8. Linux系统命令三剑客awk

    2024-04-15 02:22:03       37 阅读
  9. Qt C++ 实现文件监视源码

    2024-04-15 02:22:03       29 阅读