手写Redis缓存系统,第一章:基于http协议实现的缓存系统

关系描述

关系图文本描述

  1. main
    • 依赖 cache
    • 依赖 http
    • 流程:
      • main 包的 main 函数调用 cache.New("inmemory") 创建一个缓存实例。
      • main 包的 main 函数将缓存实例传递给 http.New(c) 创建一个 Server 实例。
      • Server 实例调用 Listen 方法启动 HTTP 服务器。
  2. http
    • 依赖 cache
    • 结构:
      • Server 结构体包含一个 cache.Cache 接口
      • Server 提供两个 HTTP 处理器:
        • cacheHandler:处理 /cache/ 路径的请求,调用 cache.Cache 接口的方法。
        • statusHandler:处理 /status 路径的请求,调用 cache.Cache 接口的 GetStat 方法。
    • 流程:
      • ServercacheHandler 处理 HTTP 方法(PUT、GET、DELETE)来操作缓存。
      • ServerstatusHandler 处理 GET 请求并返回缓存的状态信息。
  3. cache
    • 接口:
      • Cache 接口定义了 SetGetDelGetStat 方法。
    • 实现:
      • inMemoryCache 实现了 Cache 接口,提供了内存缓存的具体实现。
      • Stat 类型用于存储和更新缓存的统计信息。
    • 流程:
      • New(typ string) 函数根据传入的类型返回具体的 Cache 实现(例如 inMemoryCache)。
      • inMemoryCache 提供缓存操作的方法和统计信息。

关系图绘制

+---------------------+
|        main         |
|---------------------|
| - main()            |
| - cache.New()       |
| - http.New(c).Listen()|
+---------|-----------+
          |
          v
+---------------------+
|       http          |
|---------------------|
| - New(c cache.Cache) -> *Server |
| - Listen()          |
| - statusHandler()   |
| - cacheHandler()    |
+---------|-----------+
          |
          v
+---------------------+
|      cache          |
|---------------------|
| - New(typ string) -> Cache |
| - Cache interface   |
| - Stat struct       |
| - inMemoryCache struct |
+---------|-----------+
          |
          v
+---------------------+
|  cache/inmemory_cache.go |
|---------------------|
| - inMemoryCache struct    |
| - Set()                   |
| - Get()                   |
| - Del()                   |
| - GetStat()               |
+---------|-----------+
          |
          v
+---------------------+
|  cache/stat.go      |
|---------------------|
| - Stat struct       |
| - add()             |
| - del()             |
+---------------------+

解释

  1. main
    • main 包使用 cache 包中的 New 函数创建缓存实例,并将其传递给 http 包中的 New 函数,创建 Server 实例。
  2. http
    • Server 结构体依赖 cache 包中的 Cache 接口。
    • ServercacheHandlerstatusHandler 处理具体的 HTTP 请求,分别调用 cache.Cache 接口中的方法和 GetStat 方法。
  3. cache
    • Cache 接口定义了缓存操作的方法,inMemoryCache 实现了这些方法,Stat 类型用于缓存统计信息。

下面是一个简单的 Redis 风格的内存缓存实现代码。这些代码实现了类似于 Redis 的基本功能,包括键值存储和简单的统计功能。代码结构基于你提供的代码关系图:

1. main.go

package main

import (
	"mycache/cache"
	"mycache/http"
)

func main() {
   
	c := cache.New("inmemory")
	http.New(c).Listen()
}

2. http/server.go

package http

import (
	"mycache/cache"
	"encoding/json"
	"log"
	"net/http"
)

type Server struct {
   
	cache.Cache
}

func New(c cache.Cache) *Server {
   
	return &Server{
   c}
}

func (s *Server) Listen() {
   
	http.Handle("/cache/", s.cacheHandler())
	http.Handle("/status", s
  • 25
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wade_Crab

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值