You.i Engine
YiLRUCache.h
Go to the documentation of this file.
1 // © You i Labs Inc. 2000-2017. All rights reserved.
2 #ifndef _YI_LRU_CACHE_H_
3 #define _YI_LRU_CACHE_H_
4 
6 #include "network/YiUrl.h"
7 #include "thread/YiMutex.h"
8 #include "utility/YiDateTime.h"
9 #include "utility/YiString.h"
10 
11 #include <set>
12 
13 class CYIHTTPRequest;
14 
23 struct CYILRUData
24 {
25  virtual ~CYILRUData()
26  {}
27 
29 };
30 
31 struct CYILRUCacheItem;
33 {
34  bool operator() (const CYILRUCacheItem *lhs, const CYILRUCacheItem *rhs) const;
35 };
36 
47 {
48 public:
49  CYILRUCache();
50  ~CYILRUCache();
51 
56  void Add(const CYIUrl &url,
57  std::unique_ptr<const CYILRUData> pData,
58  uint32_t uMaxAgeInSeconds = 0);
59 
63  bool Contains(const CYIUrl &url) const;
64 
68  int32_t GetSize() const;
69 
73  std::vector<CYIUrl> GetUrls() const;
74 
79  const CYILRUData *GetData(const CYIUrl &url) const;
80 
84  bool HasExpired(const CYIUrl &url) const;
85 
89  CYIDateTime GetExpiryDate(const CYIUrl &url) const;
90 
94  void Remove(const CYIUrl &url);
95 
100  const CYILRUData *Peek(const CYIUrl &url) const;
101 
105  const CYILRUData *Peek(int32_t nIndex) const;
106 
110  void SetExpireTime(uint32_t uMaxAgeInSeconds, const CYIUrl &url);
111 
115  void LogCacheItems() const;
116 
117 private:
118 
122  void Remove(CYILRUCacheItem *pItem);
123 
124  mutable std::set<CYILRUCacheItem * /* pItem */, CYILRUSortItem> m_TimeCache;
125  std::map<CYIUrl /* url */, CYILRUCacheItem * /* pItem */> m_UrlCache;
126 
127  mutable CYIMutex m_CacheMutex;
128 
130 };
131 
134 #endif // _YI_LRU_CACHE_H_
#define YI_DISALLOW_COPY_AND_ASSIGN(TypeName)
Delete the copy constructor and assignment operator (and consequently the move constructor as well) ...
Definition: YiPredef.h:114
Definition: YiMutex.h:110
Contains information required to configure and perform a HTTP request.
Definition: YiHTTPRequest.h:34
Base class for data to be stored in the cache. Derive from this class to add custom data to the cache...
Definition: YiLRUCache.h:23
This class provides a Least Recently Used (LRU) cache implementation.
Definition: YiLRUCache.h:46
A class used to encapsulate an URL.
Definition: YiUrl.h:24
Utility class that requires a string as per the following format: http://en.wikipedia.org/wiki/ISO_8601 to construct an instance and provides some commmonly used parts like year, month, hour etc. in different data types and formats. All dates are in the local timezone unless otherwise specified.
Definition: YiDateTime.h:17
virtual ~CYILRUData()
Definition: YiLRUCache.h:25
Definition: YiLRUCache.h:32
CYIUrl m_url
Definition: YiLRUCache.h:28