cache.go 1001 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package ignore
  7. import "time"
  8. type cache struct {
  9. patterns []Pattern
  10. entries map[string]cacheEntry
  11. }
  12. type cacheEntry struct {
  13. value bool
  14. access time.Time
  15. }
  16. func newCache(patterns []Pattern) *cache {
  17. return &cache{
  18. patterns: patterns,
  19. entries: make(map[string]cacheEntry),
  20. }
  21. }
  22. func (c *cache) clean(d time.Duration) {
  23. for k, v := range c.entries {
  24. if time.Since(v.access) > d {
  25. delete(c.entries, k)
  26. }
  27. }
  28. }
  29. func (c *cache) get(key string) (result, ok bool) {
  30. res, ok := c.entries[key]
  31. if ok {
  32. res.access = time.Now()
  33. c.entries[key] = res
  34. }
  35. return res.value, ok
  36. }
  37. func (c *cache) set(key string, val bool) {
  38. c.entries[key] = cacheEntry{val, time.Now()}
  39. }
  40. func (c *cache) len() int {
  41. l := len(c.entries)
  42. return l
  43. }