cache.go 658 B

12345678910111213141516171819202122232425262728293031323334
  1. package httpserver
  2. import (
  3. "fmt"
  4. "github.com/patrickmn/go-cache"
  5. )
  6. type CachedResource struct {
  7. Data []byte
  8. Ctype string
  9. Rcode int
  10. }
  11. func NewCachedResource(data []byte, ctype string, rcode int) *CachedResource {
  12. return &CachedResource{
  13. Data: data,
  14. Ctype: ctype,
  15. Rcode: rcode,
  16. }
  17. }
  18. func (c *Controller) CacheResource(key string, resource *CachedResource) {
  19. c.cache.Set(key, resource, cache.DefaultExpiration)
  20. }
  21. func (c *Controller) GetResource(key string) *CachedResource {
  22. resource, found := c.cache.Get(key)
  23. if found {
  24. fmt.Printf("Cache Hit! Found resource for URI: %s\n", key)
  25. return resource.(*CachedResource)
  26. }
  27. return nil
  28. }