2
* Copyright 2012 Google Inc.
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
// Contains DelayCache, which wraps a cache, but lets a test delay
20
// responses for specific cache-keys. The callbacks are awakened
21
// by an explicit ReleaseKey API.
23
// By default, all cache lookups are transmitted immediately to
26
// Note: MockTimeCache also supports delayed callbacks, but they are all delayed
29
#ifndef PAGESPEED_KERNEL_CACHE_DELAY_CACHE_H_
30
#define PAGESPEED_KERNEL_CACHE_DELAY_CACHE_H_
34
#include "pagespeed/kernel/base/basictypes.h"
35
#include "pagespeed/kernel/base/scoped_ptr.h"
36
#include "pagespeed/kernel/base/string.h"
37
#include "pagespeed/kernel/base/string_util.h"
38
#include "pagespeed/kernel/cache/cache_interface.h"
39
#include "pagespeed/kernel/thread/queued_worker_pool.h"
41
namespace net_instaweb {
48
class DelayCache : public CacheInterface {
50
// Note: takes ownership of nothing.
51
DelayCache(CacheInterface* cache, ThreadSystem* thread_system);
52
virtual ~DelayCache();
54
// Reimplementations of CacheInterface methods.
55
virtual void Get(const GoogleString& key, Callback* callback);
56
virtual void Put(const GoogleString& key, SharedString* value);
57
virtual void Delete(const GoogleString& key);
58
virtual void MultiGet(MultiGetRequest* request);
60
// Instructs the cache to delay delivery of callbacks for specific cache-key.
61
// It is a fatal error -- reported at class destruction, to request delay of
62
// a key that is never looked up and released.
63
void DelayKey(const GoogleString& key);
65
// Release the delay on the callback delivered for a specific key. It is
66
// ane error to attempt to release a key that was never delayed.
67
void ReleaseKey(const GoogleString& key) { ReleaseKeyInSequence(key, NULL); }
69
// See ReleaseKey. If sequence is non-NULL, the callback is
70
// delivered on the sequence, otherwise it is delivered directly
72
void ReleaseKeyInSequence(const GoogleString& key,
73
QueuedWorkerPool::Sequence* sequence);
75
static GoogleString FormatName(StringPiece name);
76
virtual GoogleString Name() const { return FormatName(cache_->Name()); }
78
virtual bool IsBlocking() const { return false; }
79
virtual bool IsHealthy() const { return cache_->IsHealthy(); }
80
virtual void ShutDown() { cache_->ShutDown(); }
84
friend class DelayCallback;
86
void LookupComplete(DelayCallback* callback);
88
typedef std::map<GoogleString, DelayCallback*> DelayMap;
90
CacheInterface* cache_;
91
scoped_ptr<AbstractMutex> mutex_;
92
StringSet delay_requests_;
95
DISALLOW_COPY_AND_ASSIGN(DelayCache);
98
} // namespace net_instaweb
100
#endif // PAGESPEED_KERNEL_CACHE_DELAY_CACHE_H_