cache

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

cache

cacheもまた、web2pyの実行環境で用意されているグローバルオブジェクトです。これは次の2つの属性を持っています:

cache a global object also available in the web2py execution environment. It has two attributes:

  • cache.ram: メインメモリ上のアプリケーションキャッシュです。

    • the application cache in main memory.

  • cache.disk: ディスク上のアプリケーションキャッシュです。

    • the application cache on disk.

cacheは呼び出し可能であり、アクションやビューをキャッシュするためのデコレータとして使うことができます。

cache is callable, this allows it to be used as a decorator for caching actions and views.

次の例では、time.ctime()関数をRAM上でキャッシュしています:

The following example caches the time.ctime() function in RAM:

1.

2.

3.

4.

def cache_in_ram():

import time

t = cache.ram('time', lambda: time.ctime(), time_expire=5)

return dict(time=t, link=A('click me', _href=request.url))

lambda: time.ctime()の出力結果は、RAM上で5秒間キャッシュされます。文字列'time'はキャッシュのキーとして使用されます。

The output of lambda: time.ctime() is cached in RAM for 5 seconds. The string 'time' is used as cache key.

次の例は、time.ctime()関数をディスク上でキャッシュします:

The following example caches the time.ctime() function on disk:

1.

2.

3.

4.

def cache_on_disk():

import time

t = cache.disk('time', lambda: time.ctime(), time_expire=5)

return dict(time=t, link=A('click me', _href=request.url))

lambda: time.ctime()の出力結果は、(shelveモジュールを用いて)ディスク上で5秒間キャッシュされます。

The output of lambda: time.ctime() is cached on disk (using the shelve module) for 5 seconds.

次の例は、time.ctime()関数をRAM上とディスク上の両方でキャッシュします:

The next example caches the time.ctime() function to both RAM and disk:

1.

2.

3.

4.

5.

6.

def cache_in_ram_and_disk():

import time

t = cache.ram('time', lambda: cache.disk('time',

lambda: time.ctime(), time_expire=5),

time_expire=5)

return dict(time=t, link=A('click me', _href=request.url))

lambda: time.ctime()の出力結果は、5秒間、(shelveモジュールを用いて)ディスク上にキャッシュされ、続いて、RAM上でキャッシュされます。web2pyは最初はRAMから、見つからない場合はディスクから探します。RAMとディスクのいずれにも存在しない場合、lambda: time.ctime()が実行され、キャッシュが更新されます。この手法はマルチプロセス環境で有用です。2つのtimeは同じである必要はありません。

The output of lambda: time.ctime() is cached on disk (using the shelve module) and then in RAM for 5 seconds. web2py looks in RAM first and if not there it looks on disk. If it is not in RAM or on disk, lambda: time.ctime() is executed and the cache is updated. This technique is useful in a multiprocess environment. The two times do not have to be the same.

次の例は、コントローラの関数の結果をRAM上にキャッシュします(ビューはキャッシュしません)。

The following example is caching in RAM the output of the controller function (but not the view):

1.

2.

3.

4.

5.

@cache(request.env.path_info, time_expire=5, cache_model=cache.ram)

def cache_controller_in_ram():

import time

t = time.ctime()

return dict(time=t, link=A('click me', _href=request.url))

cache_controller_in_ramによって返される辞書はRAM上に5秒間キャッシュされます。注意として、データベースの選択結果は、最初にシリアル化がされなければキャッシュすることができません。より良い方法は、selectメソッドのcache引数を使って、データベースを直接キャッシュすることです。

The dictionary returned by cache_controller_in_ram is cached in RAM for 5 seconds. Note that the result of a database select cannot be cached without first being serialized. A better way is to cache the database directly using the select method cache argument.

次の例は、コントローラの関数の結果をディスクにキャッシュします(ビューはキャッシュしません)。

The following example is caching the output of the controller function on disk (but not the view):

1.

2.

3.

4.

5.

6.

@cache(request.env.path_info, time_expire=5, cache_model=cache.disk)

def cache_controller_on_disk():

import time

t = time.ctime()

return dict(time=t, link=A('click to reload',

_href=request.url))

cache_controller_in_ramによって返される辞書はディスク上に5秒間キャッシュされます。なお、pickle化できないオブジェクトを含む辞書はキャッシュできないことに留意してください。

The dictionary returned by cache_controller_on_disk is cached on disk for 5 seconds. Remember that web2py cannot cache a dictionary that contains un-pickleable objects.

ビューをキャッシュすることも可能です。コントローラ関数でビューをレンダリングすることによって可能です。コントローラが文字列を返すことになるからです。これは、response.render(d)を返すことで実行されます。ここでdはビューに渡すつもりであった辞書です:

It is also possible to cache the view. The trick is to render the view in the controller function, so that the controller returns a string. This is done by returning response.render(d) where dis the dictionary we intended to pass to the view:

time_expireは0に設定することができ、これによってキャッシュを強制的にリフレッシュします。Noneを設定すると内容の期限が切れるのを防ぎます。

The time_expire can be set to 0 to force a cache refresh and to None to prevent the content from ever expiring.

次のようにすると複数のキャッシュ変数を消すことができます:

You can clear one or more cache variables with

1.

cache.ram.clear(regex='...')

ここで、 regexはキャッシュから消したいキーにマッチする正規表現です。

where regex is a regular expression maching all the keys you want removed from the cache.

次の例は、(レンダリングされたビューを含む)コントローラの関数の出力をRAM上にキャッシュします:

The following example caches the output of the controller function in RAM (including the rendered view):

1.

2.

3.

4.

5.

6.

@cache(request.env.path_info, time_expire=5, cache_model=cache.ram)

def cache_controller_and_view():

import time

t = time.ctime()

d = dict(time=t, link=A('click to reload', _href=request.url))

return response.render(d)

response.render(d)は、レンダリングされるビューを、文字列で返します。そしてそれは5秒間キャッシュされます。これは最良の、そして、最速のキャッシュ方法です。

response.render(d) returns the rendered view as a string which is now cached for 5 seconds. This is the best and fastest way of caching.

また、memcacheのような他のキャッシュメカニズムを定義することも可能です。Memcacheはgluon.contrib.memcacheを介して利用可能で、第11章にて詳しく説明されています。

It is also possible to define other caching mechanisms such as memcache. Memcache is available via gluon.contrib.memcache and is discussed in more details in Chapter 11.