Tinydb 简明教程

TinyDB - Caching Query

查询缓存是 TinyDB 的一项高级功能,可以以此将查询结果缓存起来,以便优化性能。通过这种方式,当我们再次运行相同的查询时,TinyDB 不需要从存储中读取数据。我们可以将 cache_size 传递给表函数以优化查询缓存大小。

Catching query is an advanced feature of TinyDB with the help of which it caches the query result for performance optimization. In this way, when we run the same query again, TinyDB doesn’t need to read the data from the storage. We can pass the cache_size to the table function to optimize the query cache size.

Syntax

TinyDB 查询缓存的语法如下所示:

The syntax of TinyDB query caching is shown below −

table = db.table('table_name', cache_size=value)

Example

TinyDB 在给定表中创建缓存大小内存。

TinyDB creates cache size memory in given table.

from tinydb import TinyDB
db = TinyDB('student.json')
objects = db.table('Student_Detail', cache_size = 50)
objects.all()

它将生成以下 output 。请注意,缓存大小不会影响表值。

It will produce the following output. Observe that cache size does not affect the table values.

[{
   'roll_number': 1,
   'st_name': 'elen',
   'mark': 250,
   'subject': 'TinyDB',
   'address': 'delhi'
}]

我们可通过指定“cache_size = None”来设置无限缓存大小。

We can set unlimited cache size by putting "cache_size = None".

objects = db.table('Student_Detail', cache_size = None)

我们也可以通过指定“cahce_size = 0”来禁用缓存大小。

We can also disable the cache size by putting "cache_size = 0".

objects = db.table('Student_Detail', cache_size = 0)

要清除缓存大小,请使用以下查询:

To clear the cache size, use the following query −

db.clear_cache()