You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
532 B

5 years ago
  1. package govue2
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. var cache sync.Map
  7. type mcache struct {
  8. html string
  9. creation int64
  10. timeoutSec int64
  11. }
  12. func GetCache(key string) string {
  13. v, _ := cache.Load(key)
  14. if v != nil {
  15. mc := v.(*mcache)
  16. if time.Now().Unix() < mc.creation+mc.timeoutSec {
  17. //log.Println("加载缓存")
  18. return mc.html
  19. }
  20. }
  21. return ""
  22. }
  23. func SetCache(key string, val string, timeoutSec int64) {
  24. cache.Store(key, &mcache{
  25. html: val,
  26. creation: time.Now().Unix(),
  27. timeoutSec: timeoutSec,
  28. })
  29. }