当前位置:网站首页>分布式cache项目
分布式cache项目
2022-08-04 05:31:00 【wwcoding】
系统结构
1 LRU 缓存淘汰策略
Map+双向链表:
每操作一次节点,就将节点移到队尾
内存超了 就删掉队首节
实现: 结构体, get函数,removeoldest函数,新增修改函数
问题:如果对于需要周期处理的数据 ?
优化解决:
拓展:带定时器的LRU
2 并发缓存
要点:Mutex 互斥锁 实现LRU的并发控制
数据结构Group
2.1 Byteview类 作为value
- 用[]byte数组作为value(可以存储各种类型) 并实现克隆方法 读时返回克隆值
- 实现方法:Len() ByteSlice() cloneBytes() String()
知识点:深拷贝浅拷贝 浅:不拷贝对象里引用的对象
2.2 给lru添加并发特性
实例化lru 封装add和get方法 在两个方法调用时加锁
多个goroutine读写同一个变量时,用Lock()和unLock()对可能发生冲突的代码段进行保护
为lru中cache添加并发特性
- 只读时返回原value的拷贝值,原value用byte数组表示,因为可以支持任意的数据类型
- 实例化lru,在get和add中加入mutex互斥锁
2.3 实现Group结构
2.3.1 回调Getter
如果缓存不存在 通过回调函数获取源数据并添加到缓存中
实现:
- getter接口
- 函数类型GetterFunc 实现Get
接口型函数 : 可以传入函数作为参数 也能传入实现该接口的结构体作为参数
2.3.2 Group定义:缓存命名空间
结构:name getter mainCache
实现构造函数 和 getgroup函数
2.3.3 重要方法 get
缓存中有则返回
缓存中无 则调用load方法 去调用用户的回调函数 并将源数据添加至缓存mainCache中
3 HTTP服务器
3.1 http库
- 创建任意类型 server,并实现 ServeHTTP 方法。
- 调用 http.ListenAndServe 在port 端口启动 http 服务,处理请求的对象为 server
package main
import (
"log"
"net/http"
)
type server int
func (h *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path)
w.Write([]byte("Hello World!"))
}
func main() {
var s server
http.ListenAndServe("localhost:9999", &s)
}
创建GeeCache HTTP服务端
- 定义节点
服务器IP端口号 节点访问前缀basePath - 实现server方法
- 判断访问是否合法
- 获取groupName和key
- 调用getgroup函数读取group
- group.get(key)获取对应缓存
- 将缓存值写入httpResponse的body返回
4 创建客户端
4.1 一致性哈希
分布式缓存会将不同的数据缓存进不同的节点,通过一致性hash让每次key请求都能发送到缓存key的对应节点
- 方案一: hash算法 hash(key)%10 任意一个客户端请求key 都会发送到固定的节点
问题:节点数量发生变化怎么处理?移除一台节点,缓存值对应节点都发生了变化。所有缓存失效,需要重新去获取数据,引起缓存雪崩
- 方案二:一致性哈希算法
算法步骤:- 计算节点的哈希值 放在环上
- 计算key的hash,放在环上,顺时针寻到第一个节点就是应该选取的节
- 在新加入节点时,只需要重新定位一小部分数据即可
问题:数据倾斜 缓存节点间负载不均
解决:利用虚拟节点,扩充了节点数量 一个真实节点可以对应多个虚拟节点 - 计算虚拟节点的hash值,放在环上
- 计算key的hash值找到对应的虚拟节点
客户端实现
问题&解决记录
go mod对go的package进行管理
go mod init会将当前工作目录作为项目根目录go test编译报错
command-line-arguments [command-line-arguments.test]
原因:找不到依赖文件
解决:go test -v xxx.go xxx_test.go
边栏推荐
- 在AWS-EC2中安装Minikube集群
- Design and implementation of legal aid platform based on asp.net (with project link)
- 关于DG(域泛化)领域的PCL方法的代码实例
- C语言静态变量static的分析
- 安装pyspider后运行pyspider all后遇到的问题
- 结构体内存对齐-C语言
- 动态内存管理-C语言
- Deep Learning Theory - Initialization, Parameter Adjustment
- C语言结构体(必须掌握版)
- Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?
猜你喜欢
随机推荐
A code example of the PCL method in the domain of DG (Domain Generalization)
Miscellaneous [development] [VS Code] remote - SSD retry failed
arm-2-基础阶段
MySQL存储过程学习笔记(基于8.0)
LeetCode_22_Apr_4th_Week
Rules.make - suitable for viewing in edit mode
IDEA创建Servlet步骤
How to get started with MOOSE platform - an example of how to run the official tutorial
MOOSE平台使用入门攻略——如何运行官方教程的例子
[Development miscellaneous][Debug]debug into kernel
The second official example analysis of the MOOSE platform - about creating a Kernel and solving the convection-diffusion equation
LeetCode_Nov_2nd_Week
Tencent and NetEase have taken action one after another. What is the metaverse that is so popular that it is out of the circle?
Thunderbolt turns off automatic updates
[开发杂项][编辑器][代码阅读]ctags&vim
ideal life
Postgresql snapshot
[English learning][sentence] good sentence
位段-C语言
彻底删除MySQL教程