This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.cpp
executable file
·97 lines (78 loc) · 1.88 KB
/
Cache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "Cache.hpp"
namespace ip = boost::interprocess;
const std::string Cache::s_SignValid = "OK";
//Cache
Cache::Cache (std::string Path, std::string MutexName) :
m_Path (Path), m_WriteMutex (MutexName)
{
std::ifstream File;
OpenCreateRO (Path, &File);
m_Lock = ip::file_lock (Path.c_str ());
m_Read = ip::sharable_lock<ip::file_lock> (m_Lock, ip::try_to_lock_type ());
if (!m_Read.owns ()) throw UpdateByOther ();
std::vector<std::string> Data;
while (!File.eof ())
{
std::string Word;
if (!(File>>Word)) break;
Data.push_back (Word);
}
if (Data.empty () || Data[0] != s_SignValid) return;
Data.erase (Data.begin ());
m_Data = Data;
}//Cache
//~Cache
Cache::~Cache ()
{
ip::named_recursive_mutex::remove (GetMutexName ().c_str ());
}//~Cache
//Get
std::vector<std::string> Cache::Get () const
{
return m_Data;
}//Get
//Set
void Cache::Set (Getter *Data)
{
ip::named_recursive_mutex Lock (ip::open_or_create_t (),
GetMutexName ().c_str ());
ip::scoped_lock<ip::named_recursive_mutex> Locker (Lock,
ip::try_to_lock_type ());
if (!Locker.owns ()) throw UpdateByOther ();
m_Read.unlock ();
try
{
Write (Data);
}
catch (...)
{
m_Read.lock ();
throw;
}
m_Read.lock ();
}//Set
//Write
void Cache::Write (Getter *Data)
{
ip::scoped_lock<ip::file_lock> Locker (m_Lock);
using std::ios_base;
std::ofstream File (m_Path.c_str (), ios_base::out | ios_base::trunc);
if (!File) throw Err ("Could not open cache file");
std::vector<std::string> New = (*Data) ();
File<<s_SignValid;
for (size_t i=0; i != New.size (); i++)
{
File<<" "<<New[i];
}
m_Data = New;
}//Write
//GetMutexName
std::string Cache::GetMutexName () const
{
return "lochaplocha3851" + m_WriteMutex;
}//GetMutexName
//UpdateByOther
UpdateByOther::UpdateByOther () :
Err ("Another bkoder process updates the cache currently")
{
}//UpdateByOther