www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - vibed - best approach to manage central state (cached records)

reply "Laeeth Isharc" <nospamlaeeth nospam.laeeth.com> writes:
Hi.

Two questions:

1. On startup I load various indexes from file storage into 
memory in the shared static this segment, and I would like to 
access these from threads serving web requests.  The data can be 
considered immutable once loaded.

What is the best way to make this data accessible?  Obviously 
static doesn't work as the threads have their own storage (I 
think this is what is happening).  __gshared works, but is there 
a better approach?


2. This works:
	auto router = new URLRouter;
	router.get("/pricebars",&renderPricebars);
	router.get("/names",&renderTickernames);
	router.registerRestInterface(new MarketDataAPIImplementation);
	auto settings = new HTTPServerSettings;
	settings.options=HTTPServerOption.parseQueryString;
	settings.port = 8080;
	listenHTTP(settings, router);

But if I switch the REST registration and the static route 
registration ("/pricebars" and "/names") then it segfaults.  
Reason for mixing regular routes and REST is to be able to serve 
some data in CSV form.

Thanks.
Apr 11 2015
next sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
On 12/04/2015 7:24 a.m., Laeeth Isharc wrote:
 Hi.

 Two questions:

 1. On startup I load various indexes from file storage into memory in
 the shared static this segment, and I would like to access these from
 threads serving web requests.  The data can be considered immutable once
 loaded.

 What is the best way to make this data accessible?  Obviously static
 doesn't work as the threads have their own storage (I think this is what
 is happening).  __gshared works, but is there a better approach?


 2. This works:
      auto router = new URLRouter;
      router.get("/pricebars",&renderPricebars);
      router.get("/names",&renderTickernames);
      router.registerRestInterface(new MarketDataAPIImplementation);
      auto settings = new HTTPServerSettings;
      settings.options=HTTPServerOption.parseQueryString;
      settings.port = 8080;
      listenHTTP(settings, router);

 But if I switch the REST registration and the static route registration
 ("/pricebars" and "/names") then it segfaults. Reason for mixing regular
 routes and REST is to be able to serve some data in CSV form.

 Thanks.
The caches I use in e.g. web service frameworks use __gshared. As long as they are readonly most of the time, and written in one go. I see no problems threading wise.
Apr 11 2015
parent "Laeeth Isharc" <Laeeth.nospam nospam-laeeth.com> writes:
On Sunday, 12 April 2015 at 02:54:27 UTC, Rikki Cattermole wrote:
 On 12/04/2015 7:24 a.m., Laeeth Isharc wrote:
 Hi.

 Two questions:

 1. On startup I load various indexes from file storage into 
 memory in
 the shared static this segment, and I would like to access 
 these from
 threads serving web requests.  The data can be considered 
 immutable once
 loaded.

 What is the best way to make this data accessible?  Obviously 
 static
 doesn't work as the threads have their own storage (I think 
 this is what
 is happening).  __gshared works, but is there a better 
 approach?


 2. This works:
     auto router = new URLRouter;
     router.get("/pricebars",&renderPricebars);
     router.get("/names",&renderTickernames);
     router.registerRestInterface(new 
 MarketDataAPIImplementation);
     auto settings = new HTTPServerSettings;
     settings.options=HTTPServerOption.parseQueryString;
     settings.port = 8080;
     listenHTTP(settings, router);

 But if I switch the REST registration and the static route 
 registration
 ("/pricebars" and "/names") then it segfaults. Reason for 
 mixing regular
 routes and REST is to be able to serve some data in CSV form.

 Thanks.
The caches I use in e.g. web service frameworks use __gshared. As long as they are readonly most of the time, and written in one go. I see no problems threading wise.
Thanks. Laeeth.
Apr 11 2015
prev sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Saturday, 11 April 2015 at 19:24:22 UTC, Laeeth Isharc wrote:
 Hi.

 Two questions:

 1. On startup I load various indexes from file storage into 
 memory in the shared static this segment, and I would like to 
 access these from threads serving web requests.  The data can 
 be considered immutable once loaded.

 What is the best way to make this data accessible?  Obviously 
 static doesn't work as the threads have their own storage (I 
 think this is what is happening).  __gshared works, but is 
 there a better approach?
You can declare the cache as `immutable` (which is shared across threads) and assign to it using `assumeUnique()`. You just need to make sure the initialization really happens only once, i.e. do it in `main()` or in `shared static this()` as opposed to `static this()`.
Apr 12 2015
parent "Laeeth Isharc" <Laeeth.nospam nospam-laeeth.com> writes:
On Sunday, 12 April 2015 at 10:04:53 UTC, Marc Sch├╝tz wrote:
 On Saturday, 11 April 2015 at 19:24:22 UTC, Laeeth Isharc wrote:
 Hi.

 Two questions:

 1. On startup I load various indexes from file storage into 
 memory in the shared static this segment, and I would like to 
 access these from threads serving web requests.  The data can 
 be considered immutable once loaded.

 What is the best way to make this data accessible?  Obviously 
 static doesn't work as the threads have their own storage (I 
 think this is what is happening).  __gshared works, but is 
 there a better approach?
You can declare the cache as `immutable` (which is shared across threads) and assign to it using `assumeUnique()`. You just need to make sure the initialization really happens only once, i.e. do it in `main()` or in `shared static this()` as opposed to `static this()`.
Thanks. Laeeth
Apr 12 2015