I need to save information about files in some kind of variable.
Preferably I would be able to save a variable by the filepath + filename, which then contains information about the file such as content and last time it was written to.
I'm working on a webserver which handles dynamic content, so I won't know beforehand which files will be read, so I need to do this at runtime.
The idea is to implement caching, so that once a page has been read and processed, it's placed in a variable, the next time the same page is requested, it would check the file on disk for latest written timestamp and compare to the variable, if they're the same then just serve up the cached version, if they differ then process the updated file, then serve it up. Nothing mindboggling there, but I've no idea how to implement dynamic variables in Nim to let me easily track the files.
Are you talking about maps/dictionaries, where the key is a filename and the value is the Web page content?
I'm sorry, but "dynamic" means a lot of things, especially when used with "variable". How exactly is it supposed to be dynamic?
It's probably best to use an existing solution, memcached seems like a popular library. They surely have some idea on how to set this up, and network requests on a local machine are likely to be very fast. Like most seemingly simple problems, caching is a deep rabbit hole.
Possible implementation is to add the data to memcached on read, and then to set the data in memcached on file update, which should only go through your server. (if it doesn't, data could be a few minutes out of date)
Yes, like a map, dictionary, whatever the Nim implementation is.
In CFML I can call for example container[filename].content
Dynamic, as in I don't know before hand how many files will need to be stored, so the container has to be able to be resized during runtime.
So let's say someone requests the file /index.html
The program checks the map (Or whatever container) to see if it has a key by that path/name, if it does, it then checks the value of the last write to that file, something like container."/index.html".lastwrite, and compares it to the file on disk, if the values are not the same, or the key does not exist, it will read the file, process it, and place it into that key with the proper information.
If it does exist, and the values are the same, the file has not been altered since the last time it was access, so just read the value and serve that.
I could use memcached, but I'm interested in implementing a simple built-in caching solution.
This really isn't rocket surgery, I'm just caching the processed html file, nothing non-text, and I'm curious about how to do it, I just have no idea how to implement a map, or if there's something built into Nim that works similar to what I use in daily life (CFML)
Thanks, fadg44a3w4fe, I'll take a look at tables.
By that logic, there are better webservers out there, so why build one? ;)
Hm, in the example on that page, they're specifically setting up p1 and p2 to be person objects, that's what I would need to do at runtime rather than at compiletime.
Still reading though, maybe I'm just not seeing how to dynamically add/remove table values yet.
Tables definitely seems to be what I want to use, but I'm horribly confused as to how to use them.
Let's take a simple one: proc hasKeyA, B: bool I've... no idea how to actually use that. I've tried.
no idea how to actually use that.
I guess in Ruby I would write something like
import tables var age_of = initTable[string, int]() age_of["Anabel"] = 17 age_of["Nim"] = 8 var s = "Anabel" if age_of.has_key(s): echo age_of[s]
And I can do it in a few other languages as well.
No my problem is specifically how to use the hasKey proc, if someone can show me how to do that, I should be able to use the rest, since they all have a similar pattern of use.
My example was the Nim version of my Ruby idea, including hasKey proc :-)
I mentioned Ruby to make clear that programming in Nim is often very similar to other high level languages...
akh! Sorry Stefan, I retract my snarky comment, apologize, and will now go locate some coffee before commenting again.
The server software I'm working on will be free and open source, for all my stupid questions I do intend to give back to the community.
Thanks Stefan, with that information I'm now using dynamic tables, exactly the way I was hoping to :)
I owe you a beer
And I'm now caching pages to memory.
When I've fleshed out the processing more I'm going to run some tests to see how much faster this simple caching is.
Quite possibly, I want to get as much speed and power built-in as possible.
No one wants to use a slow engine, and users don't have patience for slow-loading pages, performance is king.
I'm happy with the current caching for the moment, but will be looking into ways to boost performance once I get more of the processor built.
The processor is where I'm focusing right now, I want to get it to the point where I'm supporting at least 90% of the commonmark Markdown spec, create a framework for scripting, and then do a public beta release.