Go应该如何实现二级缓存

目录
  • 二、实现连接mysql并执行查询语句

一、需求

  • 实现二级缓存
  • 程序运行起来后提示:“请输入命令:”,如果输入getall,查询并显示所有人员的信息
  • 第一次时查询mysql并将结果缓存在redis,设置60秒的过期时间
  • 以后的每次查询,如果redis有数据就从redis加载,没有则重复上一步的操作

二、实现连接mysql并执行查询语句

先实现需求二,当输入命令getall时,查询并显示所有人员的信息。

package main
​
import (
    "fmt"
    _"github.com/go-sql-driver/mysql"
    "github.com/jmoiron/sqlx"
)
​
type human struct {
    name string `db:"name"`
    age int `db:"age"`
}
func main() {
    var cmd string
    for{
        fmt.println("请输入命令:")
        fmt.scan(&cmd)
​
        switch cmd{
        case "getall":
            //显示所有人的信息
            getallpeople()
        case "exit":
            //退出程序
            goto gameover
        default:
            fmt.println("输入的命令有误,请重新输入!")
        }
    }
    gameover:
    fmt.println("game over")
​
}
​
func getallpeople()  {
    fmt.println("allpeople")
    //先尝试拿缓存
    getpeoplefromredis()
    db, _ := sqlx.connect("mysql", "root:123456@tcp(localhost:3306)/mydb")
    defer db.close()
​
    var people []human
    err := db.select(&people, "select name,age from person")
    if err!=nil{
        fmt.println("查询失败!err=",err)
    }
    fmt.println(people)
    
    cachepeople2redis(people)
}

第一步还是导包,需要在mysql驱动包前面加上下划线_,因为它只是一个驱动文件,并不需要在代码中调用它的有关api接口.
接下来的这个结构体中后面的db:”name” db:”age”一定要加反单引号,否则运行时会报错。(傻傻的编者刚开始这里就出现问题啦~)

type human struct {
    name string `db:"name"`
    age int `db:"age"`
}

然后main函数里面都是一些基本语法知识,用了switch和goto这两个内容。
接下来就是连接数据库了,这里要用到数据库扩展包sqlx,sqlx包其实最大最大的优点是在查询方面,也就是使用select时优化得比较好。比原来的使用查询方便了不止一点。

db, _ := sqlx.connect("mysql", "root:123456@tcp(localhost:3306)/mydb")

drivername:mysql,表示驱动器的名称是mysql也就上面”github.com/go-sql-driver/mysql”导入的驱动器。
datasourcename是root:123456@tcp(localhost:3306)/mydb 它的含义是 账户名:密码@tcp(ip:端口)/数据库名称。
将缓存查询结果到redis,就是通过这个函数cachepeople2redis(people)。

三、写一个错误处理函数

func handleerror(err error,why string)  {
    if err != nil{
        fmt.println(err,why)
        os.exit(1)
    }
}

因为后面需要处理很多错误,而错误处理也是go的一个特性,所以我们这先写一个错误处理函数。

四、设置二级缓存

func cachepeople2redis(people []human)  {
    conn, _ := redis.dial("tcp", "localhost:6379")
    defer conn.close()
    for _,human := range people{
        humanstr := fmt.sprint(human)
        _, err := conn.do("rpush", "people", humanstr)
        if err != nil{
            fmt.println("缓存失败(rpush people),err=",err)
            return
        }
    }
    _, err := conn.do("expire", "people", 66)
    if err!=nil{
        handleerror(err,"@expire people 60")
    }
    fmt.println("缓存成功!")
}

redis.dial()这个函数是用来连接redis的,需要给定网络协议和ip地址及端口号,redis的端口号默认为6379.
defer conn.close()表示延时结束与redis的连接,为了节省系统的io资源,需要及时关闭连接!刚入门时我们很容易忘记这个,需要我们养成习惯!
conn.do()是用来执行数据库命令的,第一个参数是命令名,后面的参数是数据库命令的参数。它返回的结果中reply是字节数组[]byte类型,需要根据具体的业务类型进行数据类型转换。
这段代码先将people数组中的每一个human放入到redis的people列表中。然后再执行expire命令,将列表设置过期时间。
执行成功!下面是运行结果:

请输入命令:
getall
allpeople
[{大扬 21} {小飞 21} {大红袍 1} {小芳 18}]
缓存成功!
请输入命令:

然后去看看数据库里面存进去没有。

127.0.0.1:6379> lrange people 0 -1
1) "{\xe5\xa4\xa7\xe6\x89\xac 21}"
2) "{\xe5\xb0\x8f\xe9\xa3\x9e 21}"
3) "{\xe5\xa4\xa7\xe7\xba\xa2\xe8\xa2\x8d 1}"
4) "{\xe5\xb0\x8f\xe8\x8a\xb3 18}"

过了一分钟之后,再查看redis数据库内的数据。

127.0.0.1:6379> lrange people 0 -1
(empty list or set)

已经消失了。

再写一个函数:

func getpeoplefromredis() (peoplestrs []string) {
    //连数据库 
    conn, _ := redis.dial("tcp", "localhost:6379")
    //延迟关闭
    defer conn.close()
    //执行命令
    reply, err := conn.do("lrange", "people", 0, -1)
    //处理错误
    handleerror(err,"@lrange people 0 -1")
    //类型转换
    peoplestrs, err = redis.strings(reply, err)
    //打印结果
    fmt.println("缓存拿取结果:",peoplestrs,err)
    return
}

如果redis里面有就不需要从mysql里面取数据了。直接从redis里面利用lrange命令来获取people的所有值。

到此这篇关于go应该如何实现二级缓存的文章就介绍到这了,更多相关go 二级缓存内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐