当前位置:网站首页>Go uses the mencached cache
Go uses the mencached cache
2022-07-30 08:02:00 【pedestrians have】
1、mencached 介绍(以下简称mem)
Memcached是一个自由开源的,高性能,分布式内存对象缓存系统.
基于内存的key-value存储,用来存储小块的任意数据(字符串、对象).这些数据可以是数据库调用、APIcall or page rendering.
一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性
Memcached 官网:https://memcached.org/
github地址 :https://github.com/memcached/memcached/wiki/Install
2、使用memThe server needs to be installed first
windowsIf you install the server, go to search,It was when I tested itubuntu的
2.1 ubuntu安装mem服务端
1/Local package index
sudo apt update
2/Install the official package
sudo apt install memcached
3/安装libmemcached-tools ,This is one that provides a variety of tools to come withMemcachedLibraries for use with the server
sudo apt install libmemcached-tools
4/This time it should start by itself,可以用命令查看一下
memcached
或者
service memcached status
或者
lsof -i :11211
5/查看端口是否存在
netstat -ntlp
----
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 18846/memcached
6/Restart and stop using commands
service memcached start
service memcached restart
service memcached stop
2.2 ubuntu修改mem配置文件
1/ 使用 which memcached 查看安装目录:一般都在 /usr/bin/memcached
2/ 配置文件目录在: /etc/
vim /etc/memcached.conf
To check the interface settings,Please find the following line in the file:
-l 127.0.0.1

3、Go 整合mem (Equivalent to the previous preparation is the server,Now need to use it)
我使用的 gin to test the cache,大部分都一样,Some packages are downloaded by themselves,也可以看我之前写的gin整合
3.1、目录结构
go-cashe
├─ main.go
├─ memcached
│ └─ mem.go
└─ README.md
3.2、主要代码:mem.go
I have written all the commonly used codes,If you don't meet your needs, you can search it online
package memcached
import (
"fmt"
"github.com/bradfitz/gomemcache/memcache"
)
//这里可以写多个,用逗号分隔
var mc = memcache.New("你的ip:11211")
//set 和add 方法的区别
// If you want to setkey不存在时,则set方法与addThe effect of the method is the same
// If you want to setkey已经存在时,则set方法与replace方法效果一样
//增加缓存 Set方法
func SetMemCasheKey(key string, value string) bool {
s := &memcache.Item{
Key: key,
Value: []byte(value),
Flags: 0, //It should mean whether multiple server addresses can obtain each other
Expiration: 100, //过期时间 秒为单位
}
err := mc.Set(s)
if err != nil {
fmt.Printf("错误: %s", err.Error())
return false
}
return true
}
//增加缓存 add方法
func SetMemCasheAdd(key string, value string) bool {
s := &memcache.Item{
Key: key,
Value: []byte(value),
Flags: 0, //It should mean whether multiple server addresses can obtain each other
Expiration: 100, //过期时间
}
err := mc.Add(s)
if err != nil {
return false
}
return true
}
//获取Key
func GetMemCasheValueString(key string) string {
item, err := mc.Get(key)
if err != nil {
return ""
}
fmt.Println("获取的key :" + item.Key)
return string(item.Value)
}
//Replace the cached value
func ReplaceMemCasheValue(key string, value string) bool {
s := &memcache.Item{
Key: key,
Value: []byte(value),
Flags: 0,
Expiration: 100,
}
err := mc.Replace(s)
if err != nil {
return false
}
return true
}
//删除key
func DeleteMemCasheValue(key string) bool {
if mc.Delete(key) != nil {
return false
}
return true
}
//删除全部
func DeleteMemCasheAll() {
mc.DeleteAll()
}
3.3、主要代码:main.go
package main
import (
"fmt"
"net/http"
"strom-huang-go/go-cashe/memcached"
"github.com/gin-gonic/gin"
)
func main() {
//测试memcache缓存
r := gin.Default()
r.GET("/memCashe/:uid", func(c *gin.Context) {
uid := c.Param("uid")
s := memcached.GetMemCasheValueString(uid)
if len(s) > 0 {
fmt.Println("缓存有值---" + s)
} else {
memcached.SetMemCasheKey(uid, "hello")
s = "hello"
}
c.JSON(http.StatusOK, gin.H{
"message": s,
})
})
r.Run("127.0.0.1:8080")
}
4、简单测试结果
go run main.go 后控制台输出
浏览器输入:
http://127.0.0.1:8080/memCashe/123

5、补充一个小知识
go run -race main.go
-race parameter is go 的 Race Detector,Built-in integration tools,Can easily check to see if there is race condition
5、Documentation code address
https://gitee.com/hjx_RuGuoYunZhiDao/strom-huang-go.git —go-cashe目录
边栏推荐
- export , export default, import complete usage
- Electron中设置菜单(Menu),主进程向渲染进程共享数据
- MySQL题外篇【ORM思想解析】
- How to understand plucker coordinates (geometric understanding)
- C#的访问修饰符,声明修饰符,关键字有哪些?扫盲篇
- 一段神奇的没有主方法的代码
- STL源码剖析:class template explicit specialization代码测试和理解
- 阿里二面:列出 Api 接口优化的几个技巧
- 识别“数据陷阱”,发现数据的可疑之处
- The terminal connection tools, rolling Xshell
猜你喜欢

How does Redis prevent oversold and inventory deduction operations?

LVM and disk quotas

Ali two sides: Sentinel vs Hystrix comparison, how to choose?

Process and Scheduled Task Management

The Geometric Meaning of Vector Cross Product and the Calculation of Modulus

MySQL master-slave replication configuration construction, one step in place

从追赶到超越,国产软件大显身手

阿里二面:Sentinel vs Hystrix 对比,如何选择?

export , export default,import完整用法

From catching up to surpassing, domestic software shows its talents
随机推荐
golang : Zap日志整合
Test Development Engineer Growth Diary 010 - CI/CD/CT in Jenkins (Continuous Integration Build/Continuous Delivery/Continuous Testing)
Playing script killing with AI: actually more involved than me
MySQL主从复制配置搭建,一步到位
golang: Gorm配置Mysql多数据源
Linx常见目录&文件管理命令&VI编辑器使用 介绍
VR机器人教你如何正确打乒乓球
AI元学习引入神经科学,医疗效果有望精准提升
Ali two sides: List several tips for Api interface optimization
Selenium01
Universal js time date format conversion
go : go gin返回JSON数据
阿里二面:Redis有几种集群方案?我答了4种
头条二面:MySQL中有几种常见的 SQL 错误用法?
bin文件夹下的roslyn文件夹
适合程序员的输入法
Graphical relational database design ideas, this is too vivid
STL源码剖析:bound friend template friend代码测试和理解
使用navicat连接mysql数据库时常报的错误:2003、1698、1251
Go 使用 freecache 缓存