Merge branch 'master' into jwi-bcc64xsingletonwarning
[ACE_TAO.git] / ACE / apps / JAWS2 / JAWS / Cache_Manager_T.h
blob36f00c3c27342495bd5ea5d8977e332f269a16bb
1 /* -*- c++ -*- */
2 // Hey Emacs! This is a C++ file!
3 #ifndef JAWS_CACHE_MANAGER_T_H
4 #define JAWS_CACHE_MANAGER_T_H
6 #include "ace/Singleton.h"
8 #include "JAWS/Cache_Object.h"
10 template <class KEY, class HASH_FUNC, class EQ_FUNC> class JAWS_Cache_Hash;
11 template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC>
12 class JAWS_Cache_Heap;
13 template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC>
14 class JAWS_Cache_List;
16 template <class KEY, class FACTORY, class HASH_FUNC, class EQ_FUNC>
17 class JAWS_Cache_Manager
19 friend class JAWS_Cache_Hash<KEY, HASH_FUNC, EQ_FUNC>;
20 friend class JAWS_Cache_Heap<KEY, FACTORY, HASH_FUNC, EQ_FUNC>;
21 friend class JAWS_Cache_List<KEY, FACTORY, HASH_FUNC, EQ_FUNC>;
23 public:
24 typedef ACE_Singleton<FACTORY, ACE_SYNCH_MUTEX> Object_Factory;
25 typedef JAWS_Cache_Hash<KEY, HASH_FUNC, EQ_FUNC> Cache_Hash;
26 typedef JAWS_Cache_List<KEY, FACTORY, HASH_FUNC, EQ_FUNC> Cache_Heap;
28 JAWS_Cache_Manager (ACE_Allocator *alloc = 0,
29 JAWS_Cache_Object_Factory *cof = 0,
31 size_t hashsize = 8192, // number of hash buckets
32 size_t maxsize = 65535, // max number of in memory
33 // objects
35 size_t maxobjsize = 256, // max cached object size in kB
36 size_t minobjsize = 0, // min cached object size in kB
38 size_t highwater = 100, // max size of cache in MB
39 size_t lowwater = 50, // min size of cache when
40 // expiring after highwater
41 // has been reached
43 int timetolive = -1, // amt of time the lowest
44 // priority item is allowed to
45 // remain in the cache
47 int counted = 0 // flag for whether to use
48 // counts
51 int open (ACE_Allocator *alloc = 0,
52 JAWS_Cache_Object_Factory *cof = 0,
54 size_t hashsize = 1024, // number of hash buckets
55 size_t maxsize = 4096, // max number of in memory
56 // objects
58 size_t maxobjsize = 5120, // max cached object size in kB
59 size_t minobjsize = 0, // min cached object size in kB
61 size_t highwater = 50, // max size of cache in MB
62 size_t lowwater = 30, // min size of cache when
63 // expiring after highwater
64 // has been reached
66 int timetolive = -1, // amount of time the lowest
67 // priority item is allowed to
68 // remain in the cache
70 int counted = 0 // flag for whether to use
71 // counts
74 ~JAWS_Cache_Manager ();
76 int close ();
78 // Search Methods
80 int GET (const KEY &key, JAWS_Cache_Object *&cobj);
81 // Retrieve the object associated with key from cache. Return 0 on
82 // success, -1 on failure.
84 int PUT (const KEY &key, const void *data, size_t size,
85 JAWS_Cache_Object *&obj);
86 // Inserts or replaces object associated with key into cache.
87 // Return 0 on success, -1 on failure.
89 int MAKE (const void *data, size_t size, JAWS_Cache_Object *&cobj);
90 // Create a cached object, increment reference count.
92 int TAKE (JAWS_Cache_Object *const &cobj);
93 // Increment reference count.
95 int DROP (JAWS_Cache_Object *&cobj);
96 // Decrement reference count on cached object, perhaps delete.
97 // Returns 0 if only decremented, 1 if deleted, -1 if error.
99 int FLUSH ();
100 // Removes lowest priority object from cache.
102 protected:
103 int GET_i (const KEY &key, JAWS_Cache_Object *&object);
104 // Retrieve the object associated with key from cache. Return 0 on
105 // success, -1 on failure.
107 int PUT_i (const KEY &key, const void *data, size_t size,
108 JAWS_Cache_Object *&object);
109 // Inserts or replaces object associated with key into cache.
110 // Return 0 on success, -1 on failure.
112 int FLUSH_i ();
113 // Removes lowest priority object from cache.
115 int FLUSH_i (const KEY &key);
116 // Removes object associated with key from cache.
118 int DROP_i (JAWS_Cache_Object *&cobj);
119 // Decrement reference count on cached object, perhaps delete.
121 private:
122 ACE_Allocator *allocator_;
123 JAWS_Cache_Object_Factory *factory_;
125 size_t hashsize_;
126 size_t maxsize_;
127 size_t maxobjsize_;
128 size_t minobjsize_;
129 size_t highwater_;
130 size_t lowwater_;
131 size_t waterlevel_;
132 int timetolive_;
133 int counted_;
135 Cache_Hash *hash_;
136 Cache_Heap *heap_;
138 ACE_SYNCH_RW_MUTEX lock_;
142 template <class KEY, class DATA, class CACHE_MANAGER>
143 class JAWS_Cache_Proxy
145 public:
146 typedef CACHE_MANAGER Cache_Manager;
147 typedef ACE_Singleton<Cache_Manager, ACE_SYNCH_MUTEX>
148 Cache_Manager_Singleton;
150 JAWS_Cache_Proxy (const KEY &, Cache_Manager * = 0);
151 // Corresponds to a GET
153 JAWS_Cache_Proxy (const KEY &, DATA *, size_t, Cache_Manager * = 0);
154 // Corresponds to a U/PUT
156 virtual ~JAWS_Cache_Proxy ();
158 DATA *data () const;
159 operator DATA * () const;
161 virtual int close (DATA *);
163 private:
164 JAWS_Cache_Object *object_;
165 Cache_Manager *manager_;
168 #include "JAWS/Cache_Manager_T.cpp"
170 #endif /* JAWS_CACHE_MANAGER_T_H */