views:

719

answers:

8

Question: What Caching Frameworks available for Delphi and how well developed are they? If there aren't any then is there a widely-accepted way of achieving the same objective? Applicable to Win32 targeting versions of Delphi.

Question Detail: The type of framework that I'm enquiring about exists largely in Web Development frameworks allowing the user to:

  • Check the Cache for previously stored Data/Object
  • Retrieve the Data/Object
  • Store the new Data/Object
  • Optionally tag the Data/Object and label it.
  • Expire Data/Objects based on some criteria (labels, tags, time limits etc).

I understand that a lack of reflection services for Delphi Objects without RTTI means that they probably won't exist in quite the same way but is there a similar way of achieving at least part of same end result in a more Delphi way?

Alternative Approach: As an alternative to a native Delphi library: Is there for example a good set of bindings for memcached or something similar?

A: 

TStringList and a hash function. (No, I am not serious)

eed3si9n
Hmm.. Not Quite what I was looking for but thanks for a small chuckle on a Monday morning!
jamiei
Why the hash function when you can use your stringlist with name=value pairs? (I am not serious either)
Lieven
+1  A: 

The caching mechanism needs hand-rolling.

Splay Trees are a useful and straightforward mechanism for storing cached objects, and also detecting how stale they are.

Will
+1  A: 

These frameworks provide some way of caching objects

Depending on your exact requirements, these might be over the top. If you try to implement a solution of your own, I'd suggest you take a look at the various containers in the Jedi VCL as a starting point.

Lieven
These frameworks might be useful for an application already using one of the above but otherwise, as you correctly pointed out, may be a little over the top.
jamiei
- kbmw, midware, the list is endless.
Marco van de Voort
+3  A: 

I have used memcached on Linux (there are versions on Windows and MacOS, as well as almost any other OS), It's quite simple.

I dealt with it directly, using indy's TIDTelnet, by reading the protocol's documentation, I only used set , get, delete, and quit.

I used this kind of commands (I set and get "name", 14 is the number of bytes to be stored):

osama@osama:~$ telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set name 0 0 14
Osama Alassiry
STORED
get name
VALUE name 0 14
Osama Alassiry
END
quit

memcached allows you to store up to 1MB per cache key, I used composite keys like 'Person|17|name', 'Person|17|picture', 'Employee|7|Salary|Basic' (these are fictitious names unrelated to what I really did) ... I have stored some binary files in the cache as base64 which allows to use up to 768k of binary data.

memcached can also be distributed on several servers by hashing the keys, and selecting one of several servers based on they hash.

Osama ALASSIRY
+1, I think this is good advise. memcached is a stable and optimized solution, which is available not only for Linux, but for Windows and Mac OS X as well. If doing it on the protocol level would be too difficult, there are client libraries (like libmemcached) that can be used as DLLs by Delphi programs.
mghie
it's very easy, I added an example. I only used set and get, delete, and quit. I had my own function to do that. I don't like to add dlls.
Osama ALASSIRY
I agree mghie - best advice so far, looking at the protocol it wouldn't be too much work to use an existing wrapper as a DLL or create a quick Delphi wrapper myself.
jamiei
+1  A: 

If you want to build your own, you probably want to do it in Freepascal, as that has working 64 bit support. Instead of binary splay trees, I'd suggest a k-ary.

Stephan Eggermont
A: 

Ok. I will probably end up feeling foolish about this, but what's the problem with the TStringList? I had been using a dynamic array of fairly structured data with a TStringList to find an element based on a string ID. Recently, I updated the TStringList to a THashedStringList. It may be a little faster, but nothing really remarkable yet. The StringList/Array arrangement has provided great performance on my application so far.

I have only been caching 100 to 150 records so far, but I expect it will work fine up to maybe a couple of thousand. In the business I am in, that is a pretty big operation.

It doesn't scale. You have 2 GByte of ram in your (32 bit) machine. That makes for a few million records. In a sorted stringlist with a million records, an insert means a block move of on average 500000 pointers (2 MByte). Hashing a string is O(length(s)).
Stephan Eggermont
Practical limits of tstringlist kick in around 40000-200000 depending on exact requirements and the grow pattern and code.
Marco van de Voort
Thanks for commenting. As I said, I am caching about 150 employee records in a payroll program. If I get up to 40,000 employees, I may have to consider the alternatives, but a StringList makes a great caching mechanism for the number of records I think I am likely to see.
+1  A: 

A Delphi client for Memcached can be found on google code:

http://code.google.com/p/delphimemcache/

Jason Southwell