Created
December 4, 2019 22:44
-
-
Save eMaringolo/bed9974d70c9ab8c6398149716e22b08 to your computer and use it in GitHub Desktop.
ZnClient subclass with cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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