91
91
This will save you a *lot* of time in subsequent sessions, because
92
92
you'll be able to use cached versions of the initial (very expensive)
93
documents. A new client will not re-request the service root at all.
95
95
>>> second_service = CookbookWebServiceClient(cache=unicode(tempdir))
97
You'll also be able to make conditional requests for many resources
98
and avoid transferring their full representations.
103
100
>>> print second_service.recipes[4].instructions
104
101
send: 'GET /1.0/recipes/4 ...
109
106
Of course, if you ever need to clear the cache directory, you'll have
110
107
to do it yourself.
112
>>> shutil.rmtree(tempdir)
117
The '1.0' version of the example web service, which we've been using up til
118
now, sets a long cache expiry time for the service root. That's why we
119
were able to create a second client that didn't request the service
120
root at all--just fetched the representations from its cache.
122
The 'devel' version of the example web service sets a cache expiry
123
time of two seconds. Let's see what that looks like on the client side.
125
>>> tempdir = tempfile.mkdtemp()
126
>>> first_service = CookbookWebServiceClient(
127
... cache=tempdir, version='devel')
128
send: 'GET /devel/ ...
131
send: 'GET /devel/ ...
135
Now let's wait for three seconds to make sure the representations become
138
>>> from time import sleep
141
When the representations are stale, a new client makes *conditional*
142
requests for the representations. If the conditions fail (as they do
143
here), the cached representations are considered to have been
144
refreshed, just as if the server had sent them again.
146
>>> second_service = CookbookWebServiceClient(
147
... cache=tempdir, version='devel')
148
send: 'GET /devel/ ...
151
send: 'GET /devel/ ...
155
Let's quickly create another client before the representation grows
158
>>> second_service = CookbookWebServiceClient(
159
... cache=tempdir, version='devel')
161
When the representations are not stale, a new client does not make any
162
HTTP requests at all--it fetches representations direct from the
112
167
>>> httplib2.debuglevel = 0
114
168
>>> shutil.rmtree(tempdir)
152
206
caps filenames at about 240 characters so that cache files can be
153
207
stored on filesystems with 255-character filename length limits. For
154
208
compatibility with eCryptfs filesystems, lazr.restfulclient goes
155
further, and caps filenames at 150 characters.
209
further, and caps filenames at 143 characters.
157
211
To test out the limit, let's create a cookbook with an incredibly
181
235
... if 'amazingly' in file]
183
237
Indeed, the filename has been truncated to fit in the rough
184
150-character safety limit for eCryptfs filesystems.
238
143-character safety limit for eCryptfs filesystems.
186
240
>>> len(cookbook_cache_filename)
189
243
Despite the truncation, some of the useful information from the
190
244
cookbook's name makes it into the filename, making it easy to find when
203
257
... price=10.22, last_printing=date)
205
259
This cookbook's URL is identical to the first cookbook's URL for far
206
longer than 150 characters. But since the truncated filename
260
longer than 143 characters. But since the truncated filename
207
261
incorporates an MD5 sum based on the full URL, the two cookbooks are
208
262
cached in separate files.