This example caches barcode images as they are generated,
to avoid generating them every time they are needed:
# /barcode/generate/12345
class BarcodeController < ApplicationController
def generate
text = params[:id]
# Retrieve barcode from fragment cache or generate it ourselves
bc = read_fragment("barcode/generate/#{text}") ||
write_fragment("barcode/generate/#{text}", Code39.to_jpeg(text))
# Write the response to the client
send_data bc, :type => 'image/jpeg', :disposition => 'inline'
end
end
Architectural Scalability | 179
In this example, we assume that the barcode for a particular piece of text never
changes, so we don??™t have to worry about cache expiration. In the real world, we
would want to expire old entries so as not to fill up all available RAM or disk space.
If we are using memcached as a fragment cache store, we do not have to worry about
this; memcached keeps the cache to the size we ask it to by throwing away the oldest
entries when it fills up.
Fragment cache stores. Like the various session stores, there are several fragment cache
stores that can hold your cached data. The default is the memory store, which is the
simplest and requires no options. It stores the fragments in the server??™s memory
space. Each Mongrel or FastCGI listener will have its own fragment cache.
The FileStore, as its name implies, stores fragments in a filesystem directory.
Pages:
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279