MST

星途 面试题库

面试题:Go语言空接口在泛型编程与并发场景下的深度应用

设计一个基于Go语言空接口实现的并发安全的泛型缓存系统。缓存系统要支持不同类型数据的存储和读取,并且在高并发环境下能够高效稳定运行。请描述系统架构设计思路,并给出关键代码示例。
29.8万 热度难度
编程语言Go

知识考点

AI 面试

面试题答案

一键面试

系统架构设计思路

  1. 缓存数据结构:使用map来存储缓存数据,由于要支持并发安全,需要使用sync.RWMutex来保护map的读写操作。map的键为字符串类型,值为空接口类型interface{},以实现泛型存储。
  2. 并发控制:利用sync.RWMutex实现读写锁,读操作使用读锁提高并发性能,写操作使用写锁保证数据一致性。
  3. 过期机制:可以引入一个time.Time类型的字段记录每个缓存项的过期时间,在读取和写入时检查过期情况并进行相应处理。

关键代码示例

package main

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

type CacheItem struct {
    Value     interface{}
    ExpiresAt time.Time
}

type Cache struct {
    data map[string]CacheItem
    mu   sync.RWMutex
}

func NewCache() *Cache {
    return &Cache{
        data: make(map[string]CacheItem),
    }
}

// Set 设置缓存项
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
    c.mu.Lock()
    defer c.mu.Unlock()
    expiresAt := time.Now().Add(duration)
    c.data[key] = CacheItem{Value: value, ExpiresAt: expiresAt}
}

// Get 获取缓存项
func (c *Cache) Get(key string) (interface{}, bool) {
    c.mu.RLock()
    item, exists := c.data[key]
    c.mu.RUnlock()
    if exists && time.Now().Before(item.ExpiresAt) {
        return item.Value, true
    }
    return nil, false
}

// Delete 删除缓存项
func (c *Cache) Delete(key string) {
    c.mu.Lock()
    defer c.mu.Unlock()
    delete(c.data, key)
}

你可以使用以下方式测试上述代码:

func main() {
    cache := NewCache()
    cache.Set("test", "value", 5*time.Second)
    value, exists := cache.Get("test")
    if exists {
        fmt.Printf("Value: %v\n", value)
    } else {
        fmt.Println("Key not found or expired")
    }
    time.Sleep(6 * time.Second)
    value, exists = cache.Get("test")
    if exists {
        fmt.Printf("Value: %v\n", value)
    } else {
        fmt.Println("Key not found or expired")
    }
}