@@ -37,7 +37,7 @@ type CacheMiddleware struct {
3737
3838// RoundTrip implements http.RoundTripper
3939func (c * CacheMiddleware ) RoundTrip (req * http.Request ) (* http.Response , error ) {
40- // Check if method is allowed
40+ // check if method is allowed
4141 methodAllowed := false
4242 for _ , m := range c .allowedMethods {
4343 if req .Method == m {
@@ -49,11 +49,10 @@ func (c *CacheMiddleware) RoundTrip(req *http.Request) (*http.Response, error) {
4949 return c .next .RoundTrip (req )
5050 }
5151
52- // Generate cache key
53- key := c .makeKey (req )
52+ key := c .makeKey (req ) // generate cache key based on request
5453
5554 c .mu .Lock ()
56- // Remove expired entries
55+ // remove expired entries
5756 for len (c .keys ) > 0 {
5857 oldestKey := c .keys [0 ]
5958 if time .Since (c .cache [oldestKey ].createdAt ) < c .ttl {
@@ -62,13 +61,12 @@ func (c *CacheMiddleware) RoundTrip(req *http.Request) (*http.Response, error) {
6261 delete (c .cache , oldestKey )
6362 c .keys = c .keys [1 :]
6463 }
65-
66- // Check cache
64+ // check cache
6765 entry , found := c .cache [key ]
6866 c .mu .Unlock ()
6967
7068 if found {
71- // Cache hit - reconstruct response
69+ // cache hit - reconstruct response
7270 return & http.Response {
7371 Status : fmt .Sprintf ("%d %s" , entry .status , http .StatusText (entry .status )),
7472 StatusCode : entry .status ,
@@ -82,44 +80,39 @@ func (c *CacheMiddleware) RoundTrip(req *http.Request) (*http.Response, error) {
8280 }, nil
8381 }
8482
85- // Fetch fresh response
83+ // fetch fresh response
8684 resp , err := c .next .RoundTrip (req )
8785 if err != nil {
8886 return resp , err
8987 }
9088
91- // Check if response code is allowed for caching
89+ // check if response code is allowed for caching
9290 if ! c .shouldCache (resp .StatusCode ) {
9391 return resp , nil
9492 }
9593
96- // Read and store response body
94+ // read and store response body
9795 body , err := io .ReadAll (resp .Body )
9896 if err != nil {
9997 return resp , err
10098 }
10199 _ = resp .Body .Close ()
102100 resp .Body = io .NopCloser (bytes .NewReader (body ))
103101
104- // Store in cache
102+ // store in cache
105103 c .mu .Lock ()
106104 defer c .mu .Unlock ()
107105
108- // Evict oldest if maxKeys reached
106+ // evict oldest if maxKeys reached
109107 if len (c .cache ) >= c .maxKeys {
110108 oldestKey := c .keys [0 ]
111109 delete (c .cache , oldestKey )
112110 c .keys = c .keys [1 :]
113111 }
114112
115- // Store new entry
116- c .cache [key ] = CacheEntry {
117- body : body ,
118- headers : resp .Header .Clone (),
119- status : resp .StatusCode ,
120- createdAt : time .Now (),
121- }
122- c .keys = append (c .keys , key ) // Maintain order
113+ // store new entry
114+ c .cache [key ] = CacheEntry {body : body , headers : resp .Header .Clone (), status : resp .StatusCode , createdAt : time .Now ()}
115+ c .keys = append (c .keys , key ) // maintain order of keys for LRU eviction
123116
124117 return resp , nil
125118}
0 commit comments