1 #ifndef UTIL_CACHE_THREAD_CLEANER__HPP
2 #define UTIL_CACHE_THREAD_CLEANER__HPP
3 
4 /*  $Id: icache_clean_thread.hpp 618367 2020-10-19 14:54:50Z grichenk $
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:  Anatoliy Kuznetsov
30  *
31  * File Description: Cache cleaner (runs a thread, calls purge to remove
32  *                   obsolete thread elements.
33  *
34  */
35 
36 #include <util/cache/icache.hpp>
37 #include <util/thread_nonstop.hpp>
38 #include <util/error_codes.hpp>
39 
40 
41 BEGIN_NCBI_SCOPE
42 
43 /// Thread class, peridically calls ICache::Purge to remove obsolete
44 /// elements
45 ///
46 class CCacheCleanerThread : public CThreadNonStop
47 {
48 public:
CCacheCleanerThread(ICache * cache,unsigned run_delay,unsigned stop_request_poll=10)49     CCacheCleanerThread(ICache* cache,
50                         unsigned run_delay,
51                         unsigned stop_request_poll = 10)
52     : CThreadNonStop(run_delay, stop_request_poll),
53       m_Cache(cache)
54     {}
55 
DoJob(void)56     virtual void DoJob(void)
57     {
58         try {
59             int timeout = m_Cache->GetTimeout();
60             m_Cache->Purge(timeout);
61         }
62         catch(exception& ex)
63         {
64             RequestStop();
65             ERR_POST_XX(Util_Cache, 3,
66                         Error << "Error when cleaning cache: "
67                               << ex.what()
68                               << " cleaning thread has been stopped.");
69         }
70     }
71 
72 private:
73     ICache*  m_Cache;
74 };
75 
76 
77 END_NCBI_SCOPE
78 
79 #endif
80