Skip to content

Commit

Permalink
Implement Cache service
Browse files Browse the repository at this point in the history
  • Loading branch information
derva committed Jan 3, 2024
1 parent a94ebea commit 1b22517
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 24 deletions.
109 changes: 95 additions & 14 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,107 @@
package cache

import "fmt"
import (
"fmt"
"net/http"
"strings"
"time"

var CacheRegister = make(map[string]string)
"github.com/derva/proxy/pkg/logger"
)

func StoreInCache(k, v string) error {
CacheRegister[k] = v
return nil
var CacheRegister = make(map[string]http.Response)
var CacheRegisterCounter = make(map[string]uint8)
var CacheTreshold uint8 = 2

func CheckLastModified(a, b string) (update bool) {
a1, _ := time.Parse(time.RFC1123, a)
b1, _ := time.Parse(time.RFC1123, b)

return a1.Before(b1)
}

func ReadFromCache(v string) bool {
val, ok := CacheRegister[v]
if ok {
fmt.Println("value is ")
fmt.Println(val)
fmt.Println(CacheRegister[v])
return true
func GetWrapper(w http.ResponseWriter, r *http.Request) http.Response {
res, err := http.Get(r.URL.String())
if err != nil {
fmt.Println("Error while fetchin data from proxy")
}

return *res
}

func CacheService(w http.ResponseWriter, req *http.Request, l logger.Logger) (http.Response, error) {
val := req.Header.Get("Cache-Control")

if len(val) == 0 {
return http.Response{}, nil
}

if strings.Contains(val, "no-store") {
l.Log("Caching is disabled, no-store directive/instructoin", true)
return http.Response{}, nil
}

var data http.Response

if IsInCache(req.URL.String()) {
data, _ = ReadFromCache(req.URL.String())
if strings.Contains(val, "no-cache") {
originRes, err := http.Head(req.URL.String())
if err != nil {
l.Log("Error while fetching HEAD data from proxy", true)
return http.Response{}, nil
}
modified := CheckLastModified(originRes.Header.Get("Last-Modified"), data.Header.Get("Last-Modified"))

if !modified {
fmt.Println("Everything up to date :) ")
return data, nil
}

}
} else {
return false
data = GetWrapper(w, req)
}

if strings.Contains(val, "no-transform") {
fmt.Println("No transform")
}

if ShouldStoreInCache(req.URL.String()) {
StoreInCache(req.URL.String(), data)
}

return data, nil
}

func ShouldStoreInCache(k string) bool {
CacheRegisterCounter[k]++
return CacheRegisterCounter[k] > CacheTreshold
}

func StoreInCache(k string, v http.Response) {
CacheRegister[k] = v
}

func FetchFromCache(v string) {
func PrintCache() {
for k, v := range CacheRegister {
fmt.Println("key value")
fmt.Println(k)
fmt.Println(v)
}
}

func IsInCache(v string) bool {
_, ok := CacheRegister[v]
return ok
}

func ReadFromCache(v string) (http.Response, error) {
val, ok := CacheRegister[v]

if !ok {
return http.Response{}, fmt.Errorf("Error: Empty response")
}

return val, nil
}
8 changes: 6 additions & 2 deletions pkg/handlers/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func ChooseAlgorithm(algo string, l logger.Logger) string {
}

func Encoding(w http.ResponseWriter, r *http.Request, l logger.Logger) string {

resp, err := http.Get(r.URL.String())
if err != nil {
l.Log("Error fetching the data from host", true)
Expand All @@ -57,10 +58,13 @@ func Encoding(w http.ResponseWriter, r *http.Request, l logger.Logger) string {

val, ok := r.Header["Accept-Encoding"]

if ok {
l.Log("Encoding enabled ...", false)
if !ok {
l.Log("Encoding isn't enabled", true)
return "nil"
}

l.Log("Encoding enabled ...", false)

algo := ChooseAlgorithm(val[0], l)

switch algo {
Expand Down
21 changes: 13 additions & 8 deletions pkg/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ package handlers
import (
"fmt"
"net/http"
"time"

"github.com/derva/proxy/pkg/cache"
"github.com/derva/proxy/pkg/logger"
)

func CheckLastModified(a, b string) (update bool) {
a1, _ := time.Parse(time.RFC1123, a)
b1, _ := time.Parse(time.RFC1123, b)
func GetResponse(w http.ResponseWriter, r *http.Request) *http.Response {
res, err := http.Get(r.URL.String())
if err != nil {
fmt.Println("Error while fetchin data from proxy")
}
//defer res.Body.Close()

return a1.Before(b1)
//body, _ := io.ReadAll(res.Body)

return res
}

func HandleWrapper(w http.ResponseWriter, r *http.Request) {
func HandleWrapper(w http.ResponseWriter, req *http.Request) {
l := logger.LoadLogger("proxy.log", "/var/log/")

encoding := Encoding(w, r, l)
fmt.Println(encoding)
cache.CacheService(w, req, l)

Encoding(w, req, l)
}

0 comments on commit 1b22517

Please sign in to comment.