Code:
#include <iostream>
#include <string>
#include <fstream>
#include <streambuf>
#include <cerrno>
#include <ctime>
#include <chrono>
#include "ignite/ignite.h"
#include "ignite/ignition.h"
using namespace ignite;
using namespace cache;
using namespace std;
std::string get_file_contents(const char *filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
return(std::string((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>()));
}
throw(errno);
}
void PutFile(Cache<string, string>& cache, const char * file) {
cout<<file<<endl;
string key(file);
string val = "";
auto start = std::chrono::high_resolution_clock::now();
//val=get_file_contents(file);
for (int i = 0; i < 1000; i++)
val = val + "aaaaa\n";
auto elapsed = std::chrono::high_resolution_clock::now() -
start;
long long microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
cout<<val.length()<<"
"<<microseconds<<"!!"<<endl;
start = std::chrono::high_resolution_clock::now();
cache.Put(key, val);
elapsed = std::chrono::high_resolution_clock::now() - start;
microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
cout<<"Put time: "<<" "<<key<<"
"<<microseconds<<"!!"<<endl;
start = std::chrono::high_resolution_clock::now();
val = cache.Get(key);
elapsed = std::chrono::high_resolution_clock::now() - start;
std::ofstream out("output");
out << val;
out.close();
microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
cout<<"Get time: "<<microseconds<<"
"<<val.length()<<" !!"<<endl;
std::cout << ">>> Retrieved organization instance
from cache: "
<< std::endl;
/*auto start = std::chrono::high_resolution_clock::now();
val = cache.Get(key);
auto elapsed = std::chrono::high_resolution_clock::now() -
start;
long long microseconds =
std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
cout<<"Get time: "<<microseconds<<"
"<<val.length()<<" !!"<<endl;
std::cout << ">>> Retrieved organization instance
from cache:
" << std::endl;*/
//std::cout << val << std::endl;
std::cout << std::endl;
}
int main(int argc, const char * argv[]) {
IgniteConfiguration cfg;
cfg.jvmInitMem = 512;
cfg.jvmMaxMem = 1024*2;
cfg.springCfgPath = "config/example-cache.xml";
std::cout << std::endl;
std::cout << ">>> Example started ..." << std::endl;
try {
Ignite grid = Ignition::Start(cfg);
Cache<string, string> cache =
grid.GetOrCreateCache<string,
string>("example");
// Cache<string, string>
cache = grid.GetCache<string,
string>("example");
PutFile(cache, argv[1]);
} catch (IgniteError& err) {
std::cout << "An error occurred: " <<
err.GetText() << std::endl;
}
std::cout << std::endl;
std::cout << ">>> Example finished, press any key to exit ..."
<<
std::endl;
std::cout << std::endl;
return 0;
}
--
View this message in context:
http://apache-ignite-users.70518.x6.nabble.com/C-Distributed-cache-for-caching-files-tp4158p4376.html
Sent from the Apache Ignite Users mailing list archive at Nabble.com.