mediacache.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package busmodels
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. "github.com/muesli/cache2go"
  8. )
  9. /*
  10. 图片文件或者视频文件等媒体文件的缓存信息,
  11. 在视频封面截图或者图片缩略图的生成以后才会记录.
  12. MediaPath 是视频文件或者图片文件的唯一uuid,也就是地址索引.
  13. CreatedAt 记录操作时间, 用户超时不提交信息,就删除
  14. */
  15. type BusMediaCache struct {
  16. MediaName string `json:"mediaName" gorm:"size:128;"`
  17. MediaPath string `json:"mediaPath" gorm:"size:128;"`
  18. }
  19. func MediaCacheSetup() {
  20. cache := cache2go.Cache("MediaCache")
  21. // cache2go supports a few handy callbacks and loading mechanisms.
  22. cache.SetAboutToDeleteItemCallback(func(e *cache2go.CacheItem) {
  23. fmt.Println("Deleting:", e.Key(), e.Data().(*BusMediaCache).MediaPath, e.CreatedOn())
  24. //todo delete media source
  25. fmt.Println(e.LifeSpan().Seconds())
  26. fmt.Println(e.CreatedOn().Unix())
  27. fmt.Println(time.Now().Unix())
  28. //要区分手动删除缓存和超时删除缓存. 手动删除的话,只删除缓存,不删除本地内容, 超时删除,会删除缓存和本地数据
  29. if time.Now().Unix()-e.CreatedOn().Unix() >= int64(e.LifeSpan().Seconds()) {
  30. if _, err := os.Stat(e.Data().(*BusMediaCache).MediaPath); err != nil {
  31. fmt.Println(e.Data().(*BusMediaCache).MediaName + " not existed.")
  32. }
  33. if err := os.Remove(e.Data().(*BusMediaCache).MediaPath); err != nil {
  34. fmt.Println(e.Data().(*BusMediaCache).MediaName + " delete failed.")
  35. } else {
  36. fmt.Println(e.Data().(*BusMediaCache).MediaName + " deleted.")
  37. }
  38. coverPath := strings.Split(e.Data().(*BusMediaCache).MediaPath, ".")[0] + ".jpg"
  39. if err := os.Remove(coverPath); err != nil {
  40. fmt.Println(coverPath + " delete failed.")
  41. } else {
  42. fmt.Println(coverPath + " deleted.")
  43. }
  44. } else {
  45. fmt.Println("手动删除缓存.")
  46. }
  47. })
  48. //cache.SEt
  49. }
  50. //查询缓存是否存在.
  51. func (e *BusMediaCache) GetCache() error {
  52. //添加数据
  53. cache := cache2go.Cache("MediaCache")
  54. res, err := cache.Value(e.MediaPath)
  55. if err == nil {
  56. fmt.Println("Found value in cache:", res.Data().(*BusMediaCache).MediaName)
  57. } else {
  58. fmt.Println("Error retrieving value from cache:", err)
  59. return err
  60. }
  61. return nil
  62. }
  63. func (e *BusMediaCache) AddCache() (err error) {
  64. //添加数据
  65. cache := cache2go.Cache("MediaCache")
  66. //添加 超时时间
  67. cache.Add(e.MediaPath, 1*time.Minute, e)
  68. return nil
  69. }
  70. //delelte media cache
  71. func (e *BusMediaCache) DeleteCache() (err error) {
  72. cache := cache2go.Cache("MediaCache")
  73. if _, err := cache.Delete(e.MediaPath); err != nil {
  74. return err
  75. }
  76. fmt.Println("delete cache", e.MediaName)
  77. return nil
  78. }
  79. //delelte media cache
  80. func (e *BusMediaCache) DeleteCacheKey() (err error) {
  81. cache := cache2go.Cache("MediaCache")
  82. if _, err := cache.Delete(e.MediaPath); err != nil {
  83. return err
  84. }
  85. fmt.Println("delete cache", e.MediaName)
  86. return nil
  87. }