1.Dd December 19, 2018 2.Dt SQLITE3_PCACHE_METHODS2 3 3.Os 4.Sh NAME 5.Nm sqlite3_pcache_methods2 , 6.Nm sqlite3_pcache_methods2 7.Nd Application Defined Page Cache. 8.Sh SYNOPSIS 9.Vt typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2; 10.Vt struct sqlite3_pcache_methods2 ; 11.Sh DESCRIPTION 12The sqlite3_config(SQLITE_CONFIG_PCACHE2, 13\&...) interface can register an alternative page cache implementation 14by passing in an instance of the sqlite3_pcache_methods2 structure. 15In many applications, most of the heap memory allocated by SQLite is 16used for the page cache. 17By implementing a custom page cache using this API, an application 18can better control the amount of memory consumed by SQLite, the way 19in which that memory is allocated and released, and the policies used 20to determine exactly which parts of a database file are cached and 21for how long. 22.Pp 23The alternative page cache mechanism is an extreme measure that is 24only needed by the most demanding applications. 25The built-in page cache is recommended for most uses. 26.Pp 27The contents of the sqlite3_pcache_methods2 structure are copied to 28an internal buffer by SQLite within the call to sqlite3_config. 29Hence the application may discard the parameter after the call to sqlite3_config() 30returns. 31.Pp 32The xInit() method is called once for each effective call to sqlite3_initialize() 33(usually only once during the lifetime of the process). 34The xInit() method is passed a copy of the sqlite3_pcache_methods2.pArg 35value. 36The intent of the xInit() method is to set up global data structures 37required by the custom page cache implementation. 38If the xInit() method is NULL, then the built-in default page cache 39is used instead of the application defined page cache. 40.Pp 41The xShutdown() method is called by sqlite3_shutdown(). 42It can be used to clean up any outstanding resources before process 43shutdown, if required. 44The xShutdown() method may be NULL. 45.Pp 46SQLite automatically serializes calls to the xInit method, so the xInit 47method need not be threadsafe. 48The xShutdown method is only called from sqlite3_shutdown() 49so it does not need to be threadsafe either. 50All other methods must be threadsafe in multithreaded applications. 51.Pp 52SQLite will never invoke xInit() more than once without an intervening 53call to xShutdown(). 54.Pp 55SQLite invokes the xCreate() method to construct a new cache instance. 56SQLite will typically create one cache instance for each open database 57file, though this is not guaranteed. 58The first parameter, szPage, is the size in bytes of the pages that 59must be allocated by the cache. 60szPage will always a power of two. 61The second parameter szExtra is a number of bytes of extra storage 62associated with each page cache entry. 63The szExtra parameter will a number less than 250. 64SQLite will use the extra szExtra bytes on each page to store metadata 65about the underlying database page on disk. 66The value passed into szExtra depends on the SQLite version, the target 67platform, and how SQLite was compiled. 68The third argument to xCreate(), bPurgeable, is true if the cache being 69created will be used to cache database pages of a file stored on disk, 70or false if it is used for an in-memory database. 71The cache implementation does not have to do anything special based 72with the value of bPurgeable; it is purely advisory. 73On a cache where bPurgeable is false, SQLite will never invoke xUnpin() 74except to deliberately delete a page. 75In other words, calls to xUnpin() on a cache with bPurgeable set to 76false will always have the "discard" flag set to true. 77Hence, a cache created with bPurgeable false will never contain any 78unpinned pages. 79.Pp 80The xCachesize() method may be called at any time by SQLite to set 81the suggested maximum cache-size (number of pages stored by) the cache 82instance passed as the first argument. 83This is the value configured using the SQLite "PRAGMA cache_size" 84command. 85As with the bPurgeable parameter, the implementation is not required 86to do anything with this value; it is advisory only. 87.Pp 88The xPagecount() method must return the number of pages currently stored 89in the cache, both pinned and unpinned. 90.Pp 91The xFetch() method locates a page in the cache and returns a pointer 92to an sqlite3_pcache_page object associated with that page, or a NULL 93pointer. 94The pBuf element of the returned sqlite3_pcache_page object will be 95a pointer to a buffer of szPage bytes used to store the content of 96a single database page. 97The pExtra element of sqlite3_pcache_page will be a pointer to the 98szExtra bytes of extra storage that SQLite has requested for each entry 99in the page cache. 100.Pp 101The page to be fetched is determined by the key. 102The minimum key value is 1. 103After it has been retrieved using xFetch, the page is considered to 104be "pinned". 105.Pp 106If the requested page is already in the page cache, then the page cache 107implementation must return a pointer to the page buffer with its content 108intact. 109If the requested page is not already in the cache, then the cache implementation 110should use the value of the createFlag parameter to help it determined 111what action to take: 112.Pp 113<table border=1 width=85% align=center> <tr><th> createFlag <th> Behavior 114when page is not already in cache <tr><td> 0 <td> Do not allocate a 115new page. 116Return NULL. 117<tr><td> 1 <td> Allocate a new page if it easy and convenient to do 118so. 119Otherwise return NULL. 120<tr><td> 2 <td> Make every effort to allocate a new page. 121Only return NULL if allocating a new page is effectively impossible. 122</table> 123.Pp 124SQLite will normally invoke xFetch() with a createFlag of 0 or 1. 125SQLite will only use a createFlag of 2 after a prior call with a createFlag 126of 1 failed. 127In between the to xFetch() calls, SQLite may attempt to unpin one or 128more cache pages by spilling the content of pinned pages to disk and 129synching the operating system disk cache. 130.Pp 131xUnpin() is called by SQLite with a pointer to a currently pinned page 132as its second argument. 133If the third parameter, discard, is non-zero, then the page must be 134evicted from the cache. 135If the discard parameter is zero, then the page may be discarded or 136retained at the discretion of page cache implementation. 137The page cache implementation may choose to evict unpinned pages at 138any time. 139.Pp 140The cache must not perform any reference counting. 141A single call to xUnpin() unpins the page regardless of the number 142of prior calls to xFetch(). 143.Pp 144The xRekey() method is used to change the key value associated with 145the page passed as the second argument. 146If the cache previously contains an entry associated with newKey, it 147must be discarded. 148Any prior cache entry associated with newKey is guaranteed not to be 149pinned. 150.Pp 151When SQLite calls the xTruncate() method, the cache must discard all 152existing cache entries with page numbers (keys) greater than or equal 153to the value of the iLimit parameter passed to xTruncate(). 154If any of these pages are pinned, they are implicitly unpinned, meaning 155that they can be safely discarded. 156.Pp 157The xDestroy() method is used to delete a cache allocated by xCreate(). 158All resources associated with the specified cache should be freed. 159After calling the xDestroy() method, SQLite considers the sqlite3_pcache* 160handle invalid, and will not use it with any other sqlite3_pcache_methods2 161functions. 162.Pp 163SQLite invokes the xShrink() method when it wants the page cache to 164free up as much of heap memory as possible. 165The page cache implementation is not obligated to free any memory, 166but well-behaved implementations should do their best. 167.Sh SEE ALSO 168.Xr sqlite3_config 3 , 169.Xr sqlite3_initialize 3 , 170.Xr SQLITE_CONFIG_SINGLETHREAD 3 171