From 2443bf21f5dc3f7144f0ee51d4c29c378a54f847 Mon Sep 17 00:00:00 2001 From: Juan Vanecek Date: Wed, 20 Apr 2022 19:10:37 -0300 Subject: [PATCH 1/2] enable caching directive to be conditional to resource --- ...thAmericanCurrenciesRESTfulControllerTest.class.st | 10 ++++++---- .../SouthAmericanCurrenciesRESTfulController.class.st | 8 +++++++- .../CachingDirectivesBuilderTest.class.st | 2 +- .../Stargate-Model/CachingDirectivesBuilder.class.st | 9 +++++---- ...STfulControllerRespondCreatedEntityPolicy.class.st | 2 +- .../RESTfulRequestHandlerBehavior.class.st | 11 ++++++----- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/source/Stargate-Examples-Tests/SouthAmericanCurrenciesRESTfulControllerTest.class.st b/source/Stargate-Examples-Tests/SouthAmericanCurrenciesRESTfulControllerTest.class.st index f240d7f..895ce6e 100644 --- a/source/Stargate-Examples-Tests/SouthAmericanCurrenciesRESTfulControllerTest.class.st +++ b/source/Stargate-Examples-Tests/SouthAmericanCurrenciesRESTfulControllerTest.class.st @@ -68,7 +68,8 @@ SouthAmericanCurrenciesRESTfulControllerTest >> testGetArgentinePeso [ assert: response contentLanguageTags isEmpty; deny: response varyHeaderNames includes: 'Accept-Language'; assert: response varyHeaderNames includes: 'Accept'; - assert: response entityTag equals: '"5dd07a40a75ea23fa44e641a92a6dd1ec7999a36"' asEntityTag. + assert: response entityTag equals: '"5dd07a40a75ea23fa44e641a92a6dd1ec7999a36"' asEntityTag; + assertCachingDirectivesFor: response with: #('immutable' 'public'). self withJsonFromContentsIn: response do: [ :json | self @@ -191,7 +192,8 @@ SouthAmericanCurrenciesRESTfulControllerTest >> testGetBrazilianReal [ assert: response contentLanguageTags isEmpty; assert: response varyHeaderNames includes: 'Accept'; assert: response entityTag - equals: '"13bd1a73c317563852cbb2858e39a0af77699f60"' asEntityTag. + equals: '"13bd1a73c317563852cbb2858e39a0af77699f60"' asEntityTag; + assertCachingDirectivesFor: response with: #('immutable'). self withJsonFromContentsIn: response do: [ :json | self @@ -289,7 +291,7 @@ SouthAmericanCurrenciesRESTfulControllerTest >> testGetCurrencies [ assert: response contentLanguageTags isEmpty; deny: response varyHeaderNames includes: 'Accept-Language'; assert: response varyHeaderNames includes: 'Accept'. - self assertCachingDirectivesFor: response with: #('immutable'). + self assertCachingDirectivesFor: response with: #('immutable' 'Max-Age=86400'). self withJsonFromContentsIn: response @@ -322,7 +324,7 @@ SouthAmericanCurrenciesRESTfulControllerTest >> testGetCurrenciesInSpanish [ do: [ :tag | self assert: tag equals: 'es-AR' asLanguageTag ]; assert: response varyHeaderNames includes: 'Accept-Language'; assert: response varyHeaderNames includes: 'Accept'. - self assertCachingDirectivesFor: response with: #('immutable'). + self assertCachingDirectivesFor: response with: #('immutable' 'Max-Age=86400'). self withJsonFromContentsIn: response diff --git a/source/Stargate-Examples/SouthAmericanCurrenciesRESTfulController.class.st b/source/Stargate-Examples/SouthAmericanCurrenciesRESTfulController.class.st index ee74dc8..8a3f0b9 100644 --- a/source/Stargate-Examples/SouthAmericanCurrenciesRESTfulController.class.st +++ b/source/Stargate-Examples/SouthAmericanCurrenciesRESTfulController.class.st @@ -199,7 +199,13 @@ SouthAmericanCurrenciesRESTfulController >> initializeCurrenciesRequestHandler [ include: currency isoCode ]; handleExceptionsApplying: [ :handler | handler addAsNotFoundError: NotFound ]; - directCachingWith: [ :caching | caching beImmutable ]; + directCachingWith: [ :caching | + caching + beImmutable; + when: [ :response :resource | resource isCollection ] apply: [ caching beStaleAfter: 1 day ]; + when: [ :response :resource | resource isCollection not and: [ resource symbol = '$' ] ] + apply: [ caching bePublic ] + ]; addAsSupportedLanguage: 'en-US'; addAsSupportedLanguage: 'es-AR'; build diff --git a/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st b/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st index 87c5b5b..060d26d 100644 --- a/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st +++ b/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st @@ -51,7 +51,7 @@ CachingDirectivesBuilderTest >> responseAffectedBy: directives [ | response | response := ZnResponse ok: ( ZnStringEntity text: 'theResource' ). - directives do: [ :directive | directive cull: response cull: self ]. + directives do: [ :directive | directive cull: response cull: self cull: self ]. ^ response ] diff --git a/source/Stargate-Model/CachingDirectivesBuilder.class.st b/source/Stargate-Model/CachingDirectivesBuilder.class.st index c87ef90..58ee627 100644 --- a/source/Stargate-Model/CachingDirectivesBuilder.class.st +++ b/source/Stargate-Model/CachingDirectivesBuilder.class.st @@ -18,7 +18,8 @@ CachingDirectivesBuilder >> addCacheControlNamed: aName [ condition := currentCondition. directives - add: [ :response :context | ( condition value: response ) then: [ response addCachingDirective: aName ] ] + add: + [ :response :context :resource | ( condition cull: response cull: resource ) then: [ response addCachingDirective: aName ] ] ] { #category : #private } @@ -130,13 +131,13 @@ CachingDirectivesBuilder >> doNotTransform [ { #category : #configuring } CachingDirectivesBuilder >> expireIn: aDuration [ - + | condition | condition := currentCondition. directives - add: [ :response :context | - ( condition value: response ) + add: [ :response :context :resource | + ( condition cull: response cull: resource ) then: [ response headers at: 'Expires' put: ( ZnUtils httpDate: DateAndTime now + aDuration ) ] ] ] diff --git a/source/Stargate-Model/RESTfulControllerRespondCreatedEntityPolicy.class.st b/source/Stargate-Model/RESTfulControllerRespondCreatedEntityPolicy.class.st index 2b79399..df21a41 100644 --- a/source/Stargate-Model/RESTfulControllerRespondCreatedEntityPolicy.class.st +++ b/source/Stargate-Model/RESTfulControllerRespondCreatedEntityPolicy.class.st @@ -32,7 +32,7 @@ RESTfulControllerRespondCreatedEntityPolicy >> responseFor: resource basedOn: ht entity: ( requestHandler encodeResource: resource within: requestContext ). requestHandler putEntityTagOf: resource in: response within: requestContext; - applyCachingDirectivesTo: response within: requestContext; + applyCachingDirectivesFor: resource to: response within: requestContext; putLanguageContentTagIn: response within: requestContext. ^ response ] diff --git a/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st b/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st index a44ca0a..f2be487 100644 --- a/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st +++ b/source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st @@ -18,9 +18,10 @@ Class { } { #category : #private } -RESTfulRequestHandlerBehavior >> applyCachingDirectivesTo: response within: requestContext [ +RESTfulRequestHandlerBehavior >> applyCachingDirectivesFor: aResource to: response within: requestContext [ - cachingDirectives do: [ :directive | directive cull: response cull: requestContext ] + cachingDirectives + do: [ :directive | directive cull: response cull: requestContext cull: aResource ] ] { #category : #private } @@ -122,7 +123,7 @@ RESTfulRequestHandlerBehavior >> from: httpRequest within: requestContext get: a setEntityTag: etag; addToVary: 'Accept'. self - applyCachingDirectivesTo: response within: requestContext; + applyCachingDirectivesFor: resource to: response within: requestContext; putLanguageContentTagIn: response within: requestContext. ^ response ] @@ -130,7 +131,7 @@ RESTfulRequestHandlerBehavior >> from: httpRequest within: requestContext get: a response := ZnResponse ok: ( self encodeResource: resource within: requestContext ). self putEntityTagOf: resource in: response within: requestContext. self - applyCachingDirectivesTo: response within: requestContext; + applyCachingDirectivesFor: resource to: response within: requestContext; putLanguageContentTagIn: response within: requestContext. ^ response ] @@ -188,7 +189,7 @@ RESTfulRequestHandlerBehavior >> from: httpRequest within: requestContext getCol response addToVary: 'Accept'. self paginationPolicy affect: response within: requestContext. self - applyCachingDirectivesTo: response within: requestContext; + applyCachingDirectivesFor: resourceCollection to: response within: requestContext; putLanguageContentTagIn: response within: requestContext. ^ response ] From 34cf93dc7a0b89a718e7d9f81805cd1577169018 Mon Sep 17 00:00:00 2001 From: Juan Vanecek Date: Thu, 21 Apr 2022 09:09:03 -0300 Subject: [PATCH 2/2] refactor suggested in review --- .../CachingDirectivesBuilderTest.class.st | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st b/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st index 060d26d..1204c46 100644 --- a/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st +++ b/source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st @@ -45,13 +45,20 @@ CachingDirectivesBuilderTest >> assertThereAre: aNumber directivesFrom: aBuilder self assertExpiresHeaderFor: response with: aDuration ] +{ #category : #'private - accessing' } +CachingDirectivesBuilderTest >> requestContext [ + + ^ HttpRequestContext new +] + { #category : #'private - accessing' } CachingDirectivesBuilderTest >> responseAffectedBy: directives [ - | response | + | response resource | - response := ZnResponse ok: ( ZnStringEntity text: 'theResource' ). - directives do: [ :directive | directive cull: response cull: self cull: self ]. + resource := 'theResource'. + response := ZnResponse ok: ( ZnStringEntity text: resource ). + directives do: [ :directive | directive cull: response cull: self requestContext cull: resource ]. ^ response ]