cache.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  5. Everyone is permitted to copy and distribute verbatim copies
  6. of this license document, but changing it is not allowed.
  7. http-wokou, An HTTP Proxying framework for bypassing DNS Security
  8. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. package httpserver
  21. import (
  22. "fmt"
  23. "net/http"
  24. "github.com/patrickmn/go-cache"
  25. )
  26. type CachedResource struct {
  27. Data []byte
  28. Headers *http.Header
  29. Ctype string
  30. Rcode int
  31. }
  32. func NewCachedResource(data []byte, headers *http.Header, rcode int) *CachedResource {
  33. return &CachedResource{
  34. Data: data,
  35. Headers: headers,
  36. Rcode: rcode,
  37. }
  38. }
  39. func (c *Controller) CacheResource(key string, resource *CachedResource) {
  40. fmt.Printf("Cached resource for: %s\n", key)
  41. c.cache.Set(key, resource, cache.DefaultExpiration)
  42. }
  43. func (c *Controller) GetResource(key string) *CachedResource {
  44. resource, found := c.cache.Get(key)
  45. if found {
  46. fmt.Printf("Cache Hit! Found resource for URI: %s\n", key)
  47. return resource.(*CachedResource)
  48. }
  49. return nil
  50. }