Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
file_cache.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
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
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_CACHE_FILE_CACHE_H_
20 #define PAGESPEED_KERNEL_CACHE_FILE_CACHE_H_
21 
29 #include "pagespeed/kernel/base/thread_annotations.h"
31 
32 namespace net_instaweb {
33 
34 class Hasher;
35 class MessageHandler;
36 class SlowWorker;
37 class Statistics;
38 class Timer;
39 class Variable;
40 
42 class FileCache : public CacheInterface {
43  public:
44  struct CachePolicy {
45  CachePolicy(Timer* timer, Hasher* hasher, int64 clean_interval_ms,
46  int64 target_size_bytes, int64 target_inode_count)
47  : timer(timer), hasher(hasher), clean_interval_ms(clean_interval_ms),
48  target_size_bytes(target_size_bytes),
49  target_inode_count(target_inode_count) {}
50  const Timer* timer;
51  const Hasher* hasher;
52  int64 clean_interval_ms;
53  int64 target_size_bytes;
54  int64 target_inode_count;
55  bool cleaning_enabled() { return clean_interval_ms != kDisableCleaning; }
56  private:
57 
58  };
59 
60  FileCache(const GoogleString& path, FileSystem* file_system,
61  ThreadSystem* thread_system, SlowWorker* worker,
62  CachePolicy* policy, Statistics* stats, MessageHandler* handler);
63  virtual ~FileCache();
64 
65  static void InitStats(Statistics* statistics);
66 
67  virtual void Get(const GoogleString& key, Callback* callback);
68  virtual void Put(const GoogleString& key, const SharedString& value);
69  virtual void Delete(const GoogleString& key);
70  void set_worker(SlowWorker* worker) { worker_ = worker; }
71  SlowWorker* worker() { return worker_; }
72 
73  static GoogleString FormatName() { return "FileCache"; }
74  virtual GoogleString Name() const { return FormatName(); }
75 
76  virtual bool IsBlocking() const { return true; }
77  virtual bool IsHealthy() const { return true; }
78  virtual void ShutDown() {}
79 
80  const CachePolicy* cache_policy() const { return cache_policy_.get(); }
81  CachePolicy* mutable_cache_policy() { return cache_policy_.get(); }
82  const GoogleString& path() const { return path_; }
83 
85  static const char kBytesFreedInCleanup[];
87  static const char kCleanups[];
89  static const char kDiskChecks[];
91  static const char kEvictions[];
94  static const char kSkippedCleanups[];
96  static const char kStartedCleanups[];
97  static const char kWriteErrors[];
98 
101  static const int kDisableCleaning = -1;
102 
103  private:
104  class CacheCleanFunction;
105  friend class FileCacheTest;
106  friend class CacheCleanFunction;
107 
113  bool Clean(int64 target_size_bytes, int64 target_inode_count);
114 
117  void CleanWithLocking(int64 next_clean_time_ms) LOCKS_EXCLUDED(mutex_);
118 
121  bool ShouldClean(int64* suggested_next_clean_time_ms) LOCKS_EXCLUDED(mutex_);
122 
127  void CleanIfNeeded() LOCKS_EXCLUDED(mutex_);
128 
129  bool EncodeFilename(const GoogleString& key, GoogleString* filename);
130 
131  const GoogleString path_;
132  FileSystem* file_system_;
133  SlowWorker* worker_;
134  MessageHandler* message_handler_;
135  const scoped_ptr<CachePolicy> cache_policy_;
136  scoped_ptr<AbstractMutex> mutex_;
137  int64 next_clean_ms_ GUARDED_BY(mutex_);
138  int path_length_limit_;
139  GoogleString clean_time_path_;
141  GoogleString clean_lock_path_;
144  FileSystem::ProgressNotifier* notifier_for_tests_;
145 
146  Variable* disk_checks_;
147  Variable* cleanups_;
148  Variable* evictions_;
149  Variable* bytes_freed_in_cleanup_;
150  Variable* skipped_cleanups_;
151  Variable* started_cleanups_;
152  Variable* write_errors_;
153 
155  static const char kCleanTimeName[];
157  static const char kCleanLockName[];
158 
161  static const int kLockTimeoutMs;
162 
163 
164 };
165 
166 }
167 
168 #endif
static const char kEvictions[]
Files evicted from cache during cleanup.
Definition: file_cache.h:91
Abstract interface for a cache.
Definition: cache_interface.h:32
Definition: statistics.h:43
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
virtual bool IsBlocking() const
Definition: file_cache.h:76
virtual void Put(const GoogleString &key, const SharedString &value)
virtual bool IsHealthy() const
Definition: file_cache.h:77
static const char kSkippedCleanups[]
Definition: file_cache.h:94
virtual void ShutDown()
Definition: file_cache.h:78
Definition: scoped_ptr.h:30
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
virtual GoogleString Name() const
Definition: file_cache.h:74
static const int kDisableCleaning
Definition: file_cache.h:101
Definition: file_system.h:76
virtual void Get(const GoogleString &key, Callback *callback)
Definition: shared_string.h:40
Definition: file_cache.h:44
const CachePolicy * cache_policy() const
Definition: file_cache.h:80
Definition: cache_interface.h:42
Definition: thread_system.h:40
Definition: message_handler.h:39
static const char kDiskChecks[]
Number of times we checked disk usage in preparation from cleanup.
Definition: file_cache.h:89
static const char kCleanups[]
Number of times we actually cleaned cache because usage was high enough.
Definition: file_cache.h:87
Simple C++ implementation of file cache.
Definition: file_cache.h:42
static const char kBytesFreedInCleanup[]
Variable names.
Definition: file_cache.h:85
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
static const char kStartedCleanups[]
Number of times we scanned the cache to see if it needed cleaning.
Definition: file_cache.h:96
Definition: hasher.h:30
See file comment.
Definition: slow_worker.h:35