NCBI C++ ToolKit
my_ncbi_cache.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef MY_NCBI_CACHE__HPP
2 #define MY_NCBI_CACHE__HPP
3 
4 /* $Id: my_ncbi_cache.hpp 101029 2023-10-18 12:33:48Z satskyse $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Sergey Satskiy
30  *
31  * File Description:
32  *
33  */
34 
35 
36 #include <mutex>
37 #include <map>
38 #include <list>
39 #include <string>
40 #include <optional>
41 using namespace std;
42 
43 #include "pubseq_gateway_types.hpp"
44 #include "myncbi_callback.hpp"
46 
48 
49 
50 class CPubseqGatewayApp;
51 
52 
54 {
55  public:
57  ePSGS_Ready, // user info is available
58  ePSGS_InProgress // somebody has requested user info and there is still no answer
59  };
60 
61  public:
63  m_LastTouch(psg_clock_t::now()),
64  m_Status(ePSGS_InProgress)
65  {}
66 
68  {}
69 
70  public:
72  {
76  };
77 
78  void x_AddToWaitList(IPSGS_Processor * processor,
79  TMyNCBIDataCB data_cb,
80  TMyNCBIErrorCB error_cb)
81  {
82  m_WaitList.push_back(SMyNCBIWaitListItem{processor, data_cb, error_cb});
83  }
84  void x_OnSuccess(const string & cookie,
85  const CPSG_MyNCBIRequest_WhoAmI::SUserInfo & user_info);
86  void x_OnError(const string & cookie, CRequestStatus::ECode status,
87  int code, EDiagSev severity, const string & message);
88  void x_OnNotFound(const string & cookie);
89  void x_RemoveWaiter(IPSGS_Processor * processor);
90 
91  public:
95  list<SMyNCBIWaitListItem> m_WaitList;
96 };
97 
98 
99 
101 {
102  public:
104  {
107  };
108 
109  public:
110  CMyNCBIOKCache(CPubseqGatewayApp * app, size_t high_mark, size_t low_mark) :
111  m_App(app), m_HighMark(high_mark), m_LowMark(low_mark)
112  {}
113 
115  {}
116 
117  public:
118  optional<SUserInfoItem> GetUserInfo(IPSGS_Processor * processor,
119  const string & cookie,
120  TMyNCBIDataCB data_cb,
121  TMyNCBIErrorCB error_cb);
122  void AddUserInfo(const string & cookie,
123  const CPSG_MyNCBIRequest_WhoAmI::SUserInfo & user_info);
124  void OnError(const string & cookie, CRequestStatus::ECode status,
125  int code, EDiagSev severity, const string & message);
126  void OnNotFound(const string & cookie);
127 
128  void ClearWaitingProcessor(const string & cookie,
129  IPSGS_Processor * processor);
130  void ClearInitiatedRequest(const string & cookie);
131 
132  size_t Size(void)
133  {
134  size_t size = 0;
135  lock_guard<mutex> guard(m_Lock);
136 
137  size = m_Cache.size();
138  return size;
139  }
140 
141  // Cleans up the cache if needed
142  void Maintain(void);
143 
144  private:
146 
147  size_t m_HighMark;
148  size_t m_LowMark;
149  map<string, SMyNCBIOKCacheItem> m_Cache;
150  list<string> m_LRU;
151  mutex m_Lock;
152 };
153 
154 
155 
156 
158 {
159  public:
161  size_t high_mark, size_t low_mark,
162  size_t expiration_sec) :
163  m_App(app), m_HighMark(high_mark), m_LowMark(low_mark),
164  m_ExpirationMs(expiration_sec * 1000)
165  {}
166 
168  {}
169 
170  public:
171  bool IsNotFound(const string & cookie);
172  void AddNotFound(const string & cookie);
173 
174  size_t Size(void)
175  {
176  size_t size = 0;
177  lock_guard<mutex> guard(m_Lock);
178 
179  size = m_Cache.size();
180  return size;
181  }
182 
183  // Cleans up the cache if needed
184  void Maintain(void);
185 
186  private:
188 
189  size_t m_HighMark;
190  size_t m_LowMark;
192 
193  map<string, psg_time_point_t> m_Cache;
194  list<string> m_LRU;
195  mutex m_Lock;
196 };
197 
198 
199 
201 {
202  public:
204  EDiagSev severity, const string & message) :
205  m_LastTouch(psg_clock_t::now()),
206  m_Status(status), m_Code(code), m_Severity(severity), m_Message(message)
207  {}
208 
210  {}
211 
213  {}
214 
215  public:
217 
218  // Error information
220  int m_Code;
222  string m_Message;
223 };
224 
225 
227 {
228  public:
230  size_t high_mark, size_t low_mark,
231  size_t back_off_ms) :
232  m_App(app), m_HighMark(high_mark), m_LowMark(low_mark),
233  m_BackOffMs(back_off_ms)
234  {}
235 
237  {}
238 
239  public:
240  optional<SMyNCBIErrorCacheItem> GetError(const string & cookie);
241  void AddError(const string & cookie, CRequestStatus::ECode status,
242  int code, EDiagSev severity, const string & message);
243 
244  size_t Size(void)
245  {
246  size_t size = 0;
247  lock_guard<mutex> guard(m_Lock);
248 
249  size = m_Cache.size();
250  return size;
251  }
252 
253  // Cleans up the cache if needed
254  void Maintain(void);
255 
256  private:
258 
259  size_t m_HighMark;
260  size_t m_LowMark;
261  size_t m_BackOffMs;
262 
263  map<string, SMyNCBIErrorCacheItem> m_Cache;
264  list<string> m_LRU;
265  mutex m_Lock;
266 };
267 
268 
269 #endif
270 
CMyNCBIErrorCache(CPubseqGatewayApp *app, size_t high_mark, size_t low_mark, size_t back_off_ms)
list< string > m_LRU
CPubseqGatewayApp * m_App
map< string, SMyNCBIErrorCacheItem > m_Cache
list< string > m_LRU
CPubseqGatewayApp * m_App
CMyNCBINotFoundCache(CPubseqGatewayApp *app, size_t high_mark, size_t low_mark, size_t expiration_sec)
map< string, psg_time_point_t > m_Cache
list< string > m_LRU
size_t Size(void)
CMyNCBIOKCache(CPubseqGatewayApp *app, size_t high_mark, size_t low_mark)
CPubseqGatewayApp * m_App
map< string, SMyNCBIOKCacheItem > m_Cache
Interface class (and self-factory) for request processor objects that can retrieve data from a given ...
EDiagSev
Severity level for the posted diagnostics.
Definition: ncbidiag.hpp:650
USING_NCBI_SCOPE
function< void(const string &cookie, CPSG_MyNCBIRequest_WhoAmI::SUserInfo info)> TMyNCBIDataCB
function< void(const string &cookie, CRequestStatus::ECode status, int code, EDiagSev severity, const string &message)> TMyNCBIErrorCB
const struct ncbi::grid::netcache::search::fields::SIZE size
chrono::steady_clock psg_clock_t
psg_clock_t::time_point psg_time_point_t
CPSG_MyNCBIRequest_WhoAmI::SUserInfo m_UserInfo
SMyNCBIOKCacheItem::EPSGS_MyNCBIResolutionStatus m_Status
psg_time_point_t m_LastTouch
SMyNCBIErrorCacheItem(CRequestStatus::ECode status, int code, EDiagSev severity, const string &message)
CRequestStatus::ECode m_Status
EPSGS_MyNCBIResolutionStatus m_Status
CPSG_MyNCBIRequest_WhoAmI::SUserInfo m_UserInfo
void x_AddToWaitList(IPSGS_Processor *processor, TMyNCBIDataCB data_cb, TMyNCBIErrorCB error_cb)
psg_time_point_t m_LastTouch
list< SMyNCBIWaitListItem > m_WaitList
Definition: inftrees.h:24
Modified on Fri Sep 20 14:57:49 2024 by modify_doxy.py rev. 669887