go使用memcached需要第三方的驱动库,这里有一个库是memcached作者亲自实现的,代码质量效率肯定会有保障
1:安装

go get github.com/bradfitz/gomemcache/memcache
1
2:使用

import "github.com/bradfitz/gomemcache/memcache"
1
3:一个栗子

package main

import (
"fmt"
"github.com/bradfitz/gomemcache/memcache"
)

var (
server = "127.0.0.1:11211"
)

func main() {
//create a handle
mc := memcache.New(server)
if mc == nil {
fmt.Println("memcache New failed")
}

//set key-value
mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value")})

//get key's value
it, _ := mc.Get("foo")
if string(it.Key) == "foo" {
fmt.Println("value is ", string(it.Value))
} else {
fmt.Println("Get failed")
}
///Add a new key-value
mc.Add(&memcache.Item{Key: "foo", Value: []byte("bluegogo")})
it, err := mc.Get("foo")
if err != nil {
fmt.Println("Add failed")
} else {
if string(it.Key) == "foo" {
fmt.Println("Add value is ", string(it.Value))
} else {
fmt.Println("Get failed")
}
}
//replace a key's value
mc.Replace(&memcache.Item{Key: "foo", Value: []byte("mobike")})
it, err = mc.Get("foo")
if err != nil {
fmt.Println("Replace failed")
} else {
if string(it.Key) == "foo" {
fmt.Println("Replace value is ", string(it.Value))
} else {
fmt.Println("Replace failed")
}
}
//delete an exist key
err = mc.Delete("foo")
if err != nil {
fmt.Println("Delete failed:", err.Error())
}
//incrby
err = mc.Set(&memcache.Item{Key: "aaa", Value: []byte("1")})
if err != nil {
fmt.Println("Set failed :", err.Error())
}
it, err = mc.Get("foo")
if err != nil {
fmt.Println("Get failed ", err.Error())
} else {
fmt.Println("src value is:", it.Value)
}
value, err := mc.Increment("aaa", 7)
if err != nil {
fmt.Println("Increment failed")
} else {
fmt.Println("after increment the value is :", value)
}
//decrby
value, err = mc.Decrement("aaa", 4)
if err != nil {
fmt.Println("Decrement failed", err.Error())
} else {
fmt.Println("after decrement the value is ", value)
}
}
---------------------
作者:代码界的小姑娘
来源:CSDN
原文:https://blog.csdn.net/weixin_37696997/article/details/78760397
版权声明:本文为博主原创文章,转载请附上博文链接!

golang操作memcached 转自https://blog.csdn.net/weixin_37696997/article/details/78760397的更多相关文章

  1. Golang拼接字符串的5种方法及其效率_Chrispink-CSDN博客_golang 字符串拼接效率 https://blog.csdn.net/m0_37422289/article/details/103362740

    Different ways to concatenate two strings in Golang - GeeksforGeeks https://www.geeksforgeeks.org/di ...

  2. https://blog.csdn.net/u011489043/article/details/68488459

    转自https://blog.csdn.net/u011489043/article/details/68488459 String 字符串常量   StringBuffer 字符串变量(线程安全) ...

  3. 转-java编译时error: illegal character '\ufeff' 的解决办法-https://blog.csdn.net/t518vs20s/article/details/80833061

    原文链接:https://blog.csdn.net/shixing_11/article/details/6976900 最近开发人员通过SVN提交了xxx.java文件,因发布时该包有问题需要回退 ...

  4. IntelliJ Idea 常用快捷键列表 (需整理下) https://blog.csdn.net/dc_726/article/details/42784275

    [常规] https://blog.csdn.net/dc_726/article/details/42784275https://jingyan.baidu.com/article/59a015e3 ...

  5. 抽象类和接口有什么区别---https://blog.csdn.net/csdn_aiyang/article/details/71171886

    https://blog.csdn.net/csdn_aiyang/article/details/71171886 概念]   抽象类.具体类是相对的,并非绝对的.抽象是一种概念性名词,具体是一种可 ...

  6. 为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212

    为什么领域模型对于架构师如此重要? https://blog.csdn.net/qq_40741855/article/details/84835212 2018年12月05日 14:30:19 绝圣 ...

  7. MVC和WebApi 使用get和post 传递参数。 转载https://blog.csdn.net/qq373591361/article/details/51508806

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq373591361/article/details/51508806我们总结一下用js请求服务器的 ...

  8. Mui本地打包笔记(一)使用AndroidStudio运行项目 转载 https://blog.csdn.net/baidu_32377671/article/details/79632411

    转载 https://blog.csdn.net/baidu_32377671/article/details/79632411 使用AndroidStudio运行HBuilder本地打包的Mui项目 ...

  9. 转载 WPF -- 控件模板 (ControlTemplate)(一) https://blog.csdn.net/qq_23018459/article/details/79899838

    ControlTemplate(控件模板)   https://blog.csdn.net/qq_23018459/article/details/79899838 WPF包含数据模板和控件模板,其中 ...

随机推荐

  1. selenium中的alter弹框

    from selenium import webdriverimport timedriver=webdriver.Chrome()driver.get('http://ui.imdsx.cn/uit ...

  2. day14 内置函数二

    lamda 语法: 函数名 = lambda 参数: 返回值注意: 1. 函数的参数可以有多个. 多个参数之间⽤逗号隔开 2. 匿名函数不管多复杂. 只能写⼀⾏, 且逻辑结束后直接返回数据 3. 返回 ...

  3. 使用WebStorm报错 Namespace 'v-bind' is not bound

    一:报错描述:                Namespace 'v-bind' is not bound.Namespace 'v-on' is not bound 等 二:问题说明:      ...

  4. where are you?

    #version_s#1.2#version_e# #update_s#https://files.cnblogs.com/files/dyh221/rank0410.zip#update_e#

  5. 汉化DevExpress

    现在的DevExpress组件包提供了TcxLocalizer,拖一个放到Form上. 然后设置相关的属性: 即可完成汉化了!怎么样,够简单!!! 需要DevChs.ini文件的朋友,点击链接加入群聊 ...

  6. GSM:嗅探语音流量

    GSM: Sniffing voice traffic I wrap up the GSM series with a walkthrough on how to decrypt voice traf ...

  7. Linux 登陆配置读取顺序

    Linux用户在登陆到Linux服务器时,一些登陆的提示欢迎信息,以及特定的环境配置等等都按预先设定好的配置来生效.Linux中的这个shell环境会读取很多不同的配置文件来达成上述目的,同时还有登陆 ...

  8. 为什么想起开通blog?

    为什么想起开通博客 2016年跨年夜写年终总结时,曾对自己许下愿,要成为一个会讲“故事”的人,无奈口才不行,写字也不好看,所以只能在电脑上码码字代替了. 在我看来,这“故事”该有许多种含义:首先,电子 ...

  9. python—DAY1

    # user = "123"# possword = "111"# count = 0## while count < 3:# user_name = i ...

  10. who are we?

    human been like animals,we work for our master. when we dreaming,we drive the machine run.