缓存系统

[[toc]]

介绍

Goravel 提供了可拓展的缓存模块。该模块可以使用 facades.Cache 进行操作。

配置

config/cache.go 中进行所有自定义配置。允许配置不同的缓存驱动,默认使用 redis,你也可以自定义驱动,可以进入配置文件进行查看。

可用的缓存驱动

名称 描述
redis Redis 驱动
custom 自定义驱动

缓存使用

从缓存中获取数据

  1. value := facades.Cache.Get("goravel", func() interface{} {
  2. return "default"
  3. })

你可以传递一个 func 作为默认值。如果指定的数据在缓存中不存在,将返回 func 的结果。传递闭包的方法允许你从数据库或其他外部服务中获取默认值。注意闭包结构 func() interface{}

  1. value := facades.Cache.Get("goravel", func() interface{} {
  2. return "default"
  3. })

检查缓存项是否存在

  1. value := facades.Cache.Has("goravel")

获取和存储

有时你可能想从缓存中获取一个数据,而当请求的缓存项不存在时,程序能为你存储一个默认值。

  1. value, err := facades.Cache.Remember("goravel", 5 * time.Second, func() interface{} {
  2. return "goravel"
  3. })

如果缓存中不存在你想要的数据时,则传递给 Remember 方法的闭包将被执行,然后将其结果返回并放置到缓存中。

你可以使用 RememberForever 方法从缓存中获取数据或者永久存储它:

  1. value, err := facades.Cache.RememberForever("goravel", func() interface{} {
  2. return "default"
  3. })

获取和删除

  1. value := facades.Cache.Pull("goravel", "default")

在缓存中存储数据

  1. err := facades.Cache.Put("goravel", "value", 5 * time.Second)

如果缓存的过期时间设置为 0, 则缓存将永久有效:

  1. err := facades.Cache.Put("goravel", "value", 0)

只存储没有的数据

Add 方法将只存储缓存中不存在的数据。如果存储成功,将返回 true ,否则返回 false

  1. res := facades.Cache.Add("goravel", "value", 5 * time.Second)

数据永久存储

Forever 方法可用于将数据持久化存储到缓存中。因为这些数据不会过期,所以必须通过 Forget 方法从缓存中手动删除它们:

  1. res := facades.Cache.Forever("goravel", "value")

从缓存中删除数据

  1. res := facades.Cache.Forget("goravel")

你可以使用 Flush 方法清空所有的缓存:

  1. res := facades.Cache.Flush()

添加自定义缓存驱动

配置

如果你想定义一个完全自定义的驱动,可以在 config/cache.go 配置文件中指定 custom 驱动类型。 然后包含一个 via 选项,实现一个 framework\contracts\cache\Store 结构:

  1. //config/cache.go
  2. "stores": map[string]interface{}{
  3. "redis": map[string]interface{}{
  4. "driver": "redis",
  5. "connection": "default",
  6. },
  7. "custom": map[string]interface{}{
  8. "driver": "custom",
  9. "via": Logger{},//自定义驱动
  10. },
  11. },

编写驱动

实现 framework\contracts\cache\Store 接口,并配置到 config/cache.go 即可。文件可以统一储存到 app/extensions 文件夹中(可修改)。

  1. //framework\contracts\cache\Store
  2. package cache
  3. import "time"
  4. type Store interface {
  5. //Get Retrieve an item from the cache by key.
  6. Get(key string, defaults interface{}) interface{}
  7. //Has Determine if an item exists in the cache.
  8. Has(key string) bool
  9. //Put Store an item in the cache for a given number of seconds.
  10. Put(key string, value interface{}, seconds time.Duration) error
  11. //Pull Retrieve an item from the cache and delete it.
  12. Pull(key string, defaults interface{}) interface{}
  13. //Add Store an item in the cache if the key does not exist.
  14. Add(key string, value interface{}, seconds time.Duration) bool
  15. //Remember Get an item from the cache, or execute the given Closure and store the result.
  16. Remember(key string, ttl time.Duration, callback func() interface{}) (interface{}, error)
  17. //RememberForever Get an item from the cache, or execute the given Closure and store the result forever.
  18. RememberForever(key string, callback func() interface{}) (interface{}, error)
  19. //Forever Store an item in the cache indefinitely.
  20. Forever(key string, value interface{}) bool
  21. //Forget Remove an item from the cache.
  22. Forget(key string) bool
  23. //Flush Remove all items from the cache.
  24. Flush() bool
  25. }