cache.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package ignore
  16. import "time"
  17. type cache struct {
  18. patterns []Pattern
  19. entries map[string]cacheEntry
  20. }
  21. type cacheEntry struct {
  22. value bool
  23. access time.Time
  24. }
  25. func newCache(patterns []Pattern) *cache {
  26. return &cache{
  27. patterns: patterns,
  28. entries: make(map[string]cacheEntry),
  29. }
  30. }
  31. func (c *cache) clean(d time.Duration) {
  32. for k, v := range c.entries {
  33. if time.Since(v.access) > d {
  34. delete(c.entries, k)
  35. }
  36. }
  37. }
  38. func (c *cache) get(key string) (result, ok bool) {
  39. res, ok := c.entries[key]
  40. if ok {
  41. res.access = time.Now()
  42. c.entries[key] = res
  43. }
  44. return res.value, ok
  45. }
  46. func (c *cache) set(key string, val bool) {
  47. c.entries[key] = cacheEntry{val, time.Now()}
  48. }
  49. func (c *cache) len() int {
  50. l := len(c.entries)
  51. return l
  52. }