1*6777b538SAndroid Build Coastguard Worker# disk_cache API 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard WorkerThe disk_cache API provides for caches that can store multiple random-access 4*6777b538SAndroid Build Coastguard Workerbyte streams of data associated with a key on disk (or in memory). 5*6777b538SAndroid Build Coastguard Worker 6*6777b538SAndroid Build Coastguard WorkerThere are two kinds of entries that can be stored: regular and sparse. 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard WorkerRegular entries contain up to 3 separate data streams. Usually stream 0 9*6777b538SAndroid Build Coastguard Workerwould be used for some kind of primary small metadata (e.g. HTTP headers); 10*6777b538SAndroid Build Coastguard Workerstream 1 would contain the main payload (e.g. HTTP body); and stream 2 would 11*6777b538SAndroid Build Coastguard Workeroptionally contain some auxiliary metadata that's needed only some of the time 12*6777b538SAndroid Build Coastguard Worker(e.g. V8 compilation cache). There is no requirement that these streams be used 13*6777b538SAndroid Build Coastguard Workerin this way, but implementations may expect similar size and usage 14*6777b538SAndroid Build Coastguard Workercharacteristics. 15*6777b538SAndroid Build Coastguard Worker 16*6777b538SAndroid Build Coastguard WorkerSparse entries have a stream 0 and a separate sparse stream that's accessed with 17*6777b538SAndroid Build Coastguard Workerspecial methods that have `Sparse` in their names. It's an API misuse to try to 18*6777b538SAndroid Build Coastguard Workeraccess streams 1 or 2 of sparse entries or to call `WriteSparseData` on entries 19*6777b538SAndroid Build Coastguard Workerthat have contents in those streams. Calling `SparseReadData` or 20*6777b538SAndroid Build Coastguard Worker`GetAvailableRange` to check whether entries are sparse is, however, permitted. 21*6777b538SAndroid Build Coastguard WorkerAn added entry becomes a regular entry once index 1 or 2 is written to, or it 22*6777b538SAndroid Build Coastguard Workerbecomes a sparse entry once the sparse stream is written to. Once that is done, 23*6777b538SAndroid Build Coastguard Workerit cannot change type and the access/modification restrictions relevant to the 24*6777b538SAndroid Build Coastguard Workertype apply. Type of an entry can always be determined using `SparseReadData` or 25*6777b538SAndroid Build Coastguard Worker`GetAvailableRange`. 26*6777b538SAndroid Build Coastguard Worker 27*6777b538SAndroid Build Coastguard WorkerThe sparse streams are named as such because they are permitted to have holes in 28*6777b538SAndroid Build Coastguard Workerthe byte ranges of contents they represent (and implementations may also drop 29*6777b538SAndroid Build Coastguard Workersome pieces independently). For example, in the case of a regular entry, 30*6777b538SAndroid Build Coastguard Workerstarting with an empty entry, and performing `WriteData` on some stream at 31*6777b538SAndroid Build Coastguard Workeroffset = 1024, length = 1024, then another `WriteData` at offset = 3072, 32*6777b538SAndroid Build Coastguard Workerlength = 1024, results in the stream having length = 4096, and the areas not 33*6777b538SAndroid Build Coastguard Workerwritten to filled in with zeroes. 34*6777b538SAndroid Build Coastguard Worker 35*6777b538SAndroid Build Coastguard WorkerIn contrast, after the same sequence of `WriteSparseData` operations, the entry 36*6777b538SAndroid Build Coastguard Workerwill actually keep track that [1024, 2048) and [3072, 4096) are valid, and will 37*6777b538SAndroid Build Coastguard Workerpermit queries with `GetAvailableRange`, and only allow reads of the defined 38*6777b538SAndroid Build Coastguard Workerranges. 39*6777b538SAndroid Build Coastguard Worker 40*6777b538SAndroid Build Coastguard Worker[`net/disk_cache/disk_cache.h`](/net/disk_cache/disk_cache.h) is the only 41*6777b538SAndroid Build Coastguard Workerinclude you need if you just want to use this API. 42*6777b538SAndroid Build Coastguard Worker`disk_cache::CreateCacheBackend()` is the first method you'll need to call. 43*6777b538SAndroid Build Coastguard Worker 44*6777b538SAndroid Build Coastguard Worker[TOC] 45*6777b538SAndroid Build Coastguard Worker 46*6777b538SAndroid Build Coastguard Worker## The implementations 47*6777b538SAndroid Build Coastguard Worker 48*6777b538SAndroid Build Coastguard Worker### [disk_cache/blockfile directory](/net/disk_cache/blockfile/) 49*6777b538SAndroid Build Coastguard Worker 50*6777b538SAndroid Build Coastguard WorkerThis implementation backs the HTTP cache on Windows. It tries to pack 51*6777b538SAndroid Build Coastguard Workermany small entries caches typically have into "block" files, which can help 52*6777b538SAndroid Build Coastguard Workerperformance but introduces a lot of complexity and makes recovery from 53*6777b538SAndroid Build Coastguard Workercorruption very tricky. 54*6777b538SAndroid Build Coastguard Worker 55*6777b538SAndroid Build Coastguard Worker### [disk_cache/memory directory](/net/disk_cache/memory/) 56*6777b538SAndroid Build Coastguard Worker 57*6777b538SAndroid Build Coastguard WorkerThis contains the in-memory-only implementation. It's used for incognito 58*6777b538SAndroid Build Coastguard Workermode. 59*6777b538SAndroid Build Coastguard Worker 60*6777b538SAndroid Build Coastguard Worker### [disk_cache/simple directory](/net/disk_cache/simple/) 61*6777b538SAndroid Build Coastguard Worker 62*6777b538SAndroid Build Coastguard WorkerThis implementation backs the HTTP cache on Android, ChromeOS, and Linux, 63*6777b538SAndroid Build Coastguard WorkermacOS and is used to implement some features like CacheStorage on all platforms. 64*6777b538SAndroid Build Coastguard WorkerThe design is centered around roughly having a single file per cache entry (more 65*6777b538SAndroid Build Coastguard Workerprecisely for streams 0 and 1), with a compact and simple in-memory index for 66*6777b538SAndroid Build Coastguard Workermembership tests, which makes it very robust against failures, but also highly 67*6777b538SAndroid Build Coastguard Workersensitive to OS file system performance. 68