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&lt;&lt;endl;
                string key(file);
                string val = &quot;&quot;;
                auto start = std::chrono::high_resolution_clock::now(); 
                //val=get_file_contents(file);
                for (int i = 0; i &lt; 1000; i++)
                                val = val + &quot;aaaaa\n&quot;; 
                auto elapsed = std::chrono::high_resolution_clock::now() - 
start;
                long long microseconds =
std::chrono::duration_cast&lt;std::chrono::microseconds>(elapsed).count();
                cout<<val.length()&lt;&lt;&quot;
&quot;&lt;&lt;microseconds&lt;&lt;&quot;!!&quot;&lt;&lt;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&lt;std::chrono::microseconds>(elapsed).count();
                cout<<"Put time: "<<" "<<key&lt;&lt;&quot;
&quot;&lt;&lt;microseconds&lt;&lt;&quot;!!&quot;&lt;&lt;endl;

                start = std::chrono::high_resolution_clock::now();
                val = cache.Get(key);
                elapsed = std::chrono::high_resolution_clock::now() - start;
                std::ofstream out(&quot;output&quot;);
                out &lt;&lt; val;
                out.close();
                microseconds =
std::chrono::duration_cast&lt;std::chrono::microseconds>(elapsed).count();
                cout<<"Get time: "<<microseconds&lt;&lt;&quot;
&quot;&lt;&lt;val.length()&lt;&lt;&quot; !!&quot;&lt;&lt;endl;
                std::cout &lt;&lt; &quot;>>> 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&lt;&lt;&quot;
&quot;&lt;&lt;val.length()&lt;&lt;&quot; !!&quot;&lt;&lt;endl;
                  std::cout &lt;&lt; &quot;>>> 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.

Reply via email to