Skip to content

Commit

Permalink
Merge pull request #155 from ba-st/cache-control-conditional-to-resource
Browse files Browse the repository at this point in the history
Improve caching directive to be able to be conditional to resource
  • Loading branch information
gcotelli authored Apr 21, 2022
2 parents 683620f + 34cf93d commit 5878e15
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions source/Stargate-Model-Tests/CachingDirectivesBuilderTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 ].
resource := 'theResource'.
response := ZnResponse ok: ( ZnStringEntity text: resource ).
directives do: [ :directive | directive cull: response cull: self requestContext cull: resource ].
^ response
]

Expand Down
9 changes: 5 additions & 4 deletions source/Stargate-Model/CachingDirectivesBuilder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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 ) ]
]
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
11 changes: 6 additions & 5 deletions source/Stargate-Model/RESTfulRequestHandlerBehavior.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -122,15 +123,15 @@ 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
]
].
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
]
Expand Down Expand Up @@ -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
]
Expand Down

0 comments on commit 5878e15

Please sign in to comment.