Skip to content

Instantly share code, notes, and snippets.

@eMaringolo
Created December 4, 2019 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eMaringolo/bed9974d70c9ab8c6398149716e22b08 to your computer and use it in GitHub Desktop.
Save eMaringolo/bed9974d70c9ab8c6398149716e22b08 to your computer and use it in GitHub Desktop.
ZnClient subclass with cache
ZnClient subclass: #ZnCachingClient
instanceVariableNames: ''
classVariableNames: ''
package: 'Zinc-HTTP-CachingClient'
execute
"Execute the currently set up request to generate a response.
If the request was performed before, retrieve the cached version.
Return the #contents of the response, if any."
^ self request method = #GET
ifTrue: [ self executeCached ]
ifFalse: [ super execute ]
executeCached
| requestHash cachedFileReference |
requestHash := (MD5 hashMessage: self request url asString) hex.
cachedFileReference := FileLocator localDirectory / 'zinc-cache'
/ requestHash.
^ cachedFileReference exists
ifTrue: [ cachedFileReference contents ]
ifFalse: [ | result |
result := super execute.
response isSuccess
ifTrue: [ cachedFileReference
ensureCreateFile;
writeStreamDo: [ :ws | ws nextPutAll: result ] ].
result ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment