]> git.ipfire.org Git - thirdparty/squid.git/blob - src/MemStore.h
Boilerplate: update copyright blurbs on src/
[thirdparty/squid.git] / src / MemStore.h
1 /*
2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_MEMSTORE_H
10 #define SQUID_MEMSTORE_H
11
12 #include "ipc/mem/Page.h"
13 #include "ipc/mem/PageStack.h"
14 #include "ipc/StoreMap.h"
15 #include "Store.h"
16
17 // StoreEntry restoration info not already stored by Ipc::StoreMap
18 struct MemStoreMapExtraItem {
19 Ipc::Mem::PageId page; ///< shared memory page with entry slice content
20 };
21 typedef Ipc::StoreMapItems<MemStoreMapExtraItem> MemStoreMapExtras;
22 typedef Ipc::StoreMap MemStoreMap;
23
24 /// Stores HTTP entities in RAM. Current implementation uses shared memory.
25 /// Unlike a disk store (SwapDir), operations are synchronous (and fast).
26 class MemStore: public Store, public Ipc::StoreMapCleaner
27 {
28 public:
29 MemStore();
30 virtual ~MemStore();
31
32 /// whether e should be kept in local RAM for possible future caching
33 bool keepInLocalMemory(const StoreEntry &e) const;
34
35 /// copy non-shared entry data of the being-cached entry to our cache
36 void write(StoreEntry &e);
37
38 /// all data has been received; there will be no more write() calls
39 void completeWriting(StoreEntry &e);
40
41 /// remove from the cache
42 void unlink(StoreEntry &e);
43
44 /// called when the entry is about to forget its association with mem cache
45 void disconnect(StoreEntry &e);
46
47 /* Store API */
48 virtual int callback();
49 virtual StoreEntry * get(const cache_key *);
50 virtual void get(String const key , STOREGETCLIENT callback, void *cbdata);
51 virtual void init();
52 virtual uint64_t maxSize() const;
53 virtual uint64_t minSize() const;
54 virtual uint64_t currentSize() const;
55 virtual uint64_t currentCount() const;
56 virtual int64_t maxObjectSize() const;
57 virtual void getStats(StoreInfoStats &stats) const;
58 virtual void stat(StoreEntry &) const;
59 virtual StoreSearch *search(String const url, HttpRequest *);
60 virtual void markForUnlink(StoreEntry &e);
61 virtual void reference(StoreEntry &);
62 virtual bool dereference(StoreEntry &, bool);
63 virtual void maintain();
64 virtual bool anchorCollapsed(StoreEntry &collapsed, bool &inSync);
65 virtual bool updateCollapsed(StoreEntry &collapsed);
66
67 static int64_t EntryLimit();
68
69 protected:
70 bool shouldCache(StoreEntry &e) const;
71 bool startCaching(StoreEntry &e);
72
73 void copyToShm(StoreEntry &e);
74 void copyToShmSlice(StoreEntry &e, Ipc::StoreMapAnchor &anchor);
75 bool copyFromShm(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
76 bool copyFromShmSlice(StoreEntry &e, const StoreIOBuffer &buf, bool eof);
77
78 void anchorEntry(StoreEntry &e, const sfileno index, const Ipc::StoreMapAnchor &anchor);
79 bool updateCollapsedWith(StoreEntry &collapsed, const sfileno index, const Ipc::StoreMapAnchor &anchor);
80
81 sfileno reserveSapForWriting(Ipc::Mem::PageId &page);
82
83 // Ipc::StoreMapCleaner API
84 virtual void noteFreeMapSlice(const Ipc::StoreMapSliceId sliceId);
85
86 private:
87 // TODO: move freeSlots into map
88 Ipc::Mem::Pointer<Ipc::Mem::PageStack> freeSlots; ///< unused map slot IDs
89 MemStoreMap *map; ///< index of mem-cached entries
90
91 typedef MemStoreMapExtras Extras;
92 Ipc::Mem::Pointer<Extras> extras; ///< IDs of pages with slice data
93
94 /// the last allocate slice for writing a store entry (during copyToShm)
95 sfileno lastWritingSlice;
96
97 /// temporary storage for slot and page ID pointers; for the waiting cache
98 class SlotAndPage
99 {
100 public:
101 SlotAndPage(): slot(NULL), page(NULL) {}
102 bool operator !() const { return !slot && !page; }
103 Ipc::Mem::PageId *slot; ///< local slot variable, waiting to be filled
104 Ipc::Mem::PageId *page; ///< local page variable, waiting to be filled
105 };
106 SlotAndPage waitingFor; ///< a cache for a single "hot" free slot and page
107 };
108
109 // Why use Store as a base? MemStore and SwapDir are both "caches".
110
111 // Why not just use a SwapDir API? That would not help much because Store has
112 // to check/update memory cache separately from the disk cache. And same API
113 // would hurt because we can support synchronous get/put, unlike the disks.
114
115 #endif /* SQUID_MEMSTORE_H */