You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_DISKCACHE_H__
  28. #define TALK_BASE_DISKCACHE_H__
  29. #include <map>
  30. #include <string>
  31. #ifdef WIN32
  32. #undef UnlockResource
  33. #endif // WIN32
  34. namespace talk_base {
  35. class StreamInterface;
  36. ///////////////////////////////////////////////////////////////////////////////
  37. // DiskCache - An LRU cache of streams, stored on disk.
  38. //
  39. // Streams are identified by a unique resource id. Multiple streams can be
  40. // associated with each resource id, distinguished by an index. When old
  41. // resources are flushed from the cache, all streams associated with those
  42. // resources are removed together.
  43. // DiskCache is designed to persist across executions of the program. It is
  44. // safe for use from an arbitrary number of users on a single thread, but not
  45. // from multiple threads or other processes.
  46. ///////////////////////////////////////////////////////////////////////////////
  47. class DiskCache {
  48. public:
  49. DiskCache();
  50. virtual ~DiskCache();
  51. bool Initialize(const std::string& folder, size_t size);
  52. bool Purge();
  53. bool LockResource(const std::string& id);
  54. StreamInterface* WriteResource(const std::string& id, size_t index);
  55. bool UnlockResource(const std::string& id);
  56. StreamInterface* ReadResource(const std::string& id, size_t index) const;
  57. bool HasResource(const std::string& id) const;
  58. bool HasResourceStream(const std::string& id, size_t index) const;
  59. bool DeleteResource(const std::string& id);
  60. protected:
  61. virtual bool InitializeEntries() = 0;
  62. virtual bool PurgeFiles() = 0;
  63. virtual bool FileExists(const std::string& filename) const = 0;
  64. virtual bool DeleteFile(const std::string& filename) const = 0;
  65. enum LockState { LS_UNLOCKED, LS_LOCKED, LS_UNLOCKING };
  66. struct Entry {
  67. LockState lock_state;
  68. mutable size_t accessors;
  69. size_t size;
  70. size_t streams;
  71. time_t last_modified;
  72. };
  73. typedef std::map<std::string, Entry> EntryMap;
  74. friend class DiskCacheAdapter;
  75. bool CheckLimit();
  76. std::string IdToFilename(const std::string& id, size_t index) const;
  77. bool FilenameToId(const std::string& filename, std::string* id,
  78. size_t* index) const;
  79. const Entry* GetEntry(const std::string& id) const {
  80. return const_cast<DiskCache*>(this)->GetOrCreateEntry(id, false);
  81. }
  82. Entry* GetOrCreateEntry(const std::string& id, bool create);
  83. void ReleaseResource(const std::string& id, size_t index) const;
  84. std::string folder_;
  85. size_t max_cache_, total_size_;
  86. EntryMap map_;
  87. mutable size_t total_accessors_;
  88. };
  89. ///////////////////////////////////////////////////////////////////////////////
  90. // CacheLock - Automatically manage locking and unlocking, with optional
  91. // rollback semantics
  92. ///////////////////////////////////////////////////////////////////////////////
  93. class CacheLock {
  94. public:
  95. CacheLock(DiskCache* cache, const std::string& id, bool rollback = false)
  96. : cache_(cache), id_(id), rollback_(rollback)
  97. {
  98. locked_ = cache_->LockResource(id_);
  99. }
  100. ~CacheLock() {
  101. if (locked_) {
  102. cache_->UnlockResource(id_);
  103. if (rollback_) {
  104. cache_->DeleteResource(id_);
  105. }
  106. }
  107. }
  108. bool IsLocked() const { return locked_; }
  109. void Commit() { rollback_ = false; }
  110. private:
  111. DiskCache* cache_;
  112. std::string id_;
  113. bool rollback_, locked_;
  114. };
  115. ///////////////////////////////////////////////////////////////////////////////
  116. } // namespace talk_base
  117. #endif // TALK_BASE_DISKCACHE_H__