Question

I'm working on a content exchanging library that requires some cache management. For example, user can publish data with

ContentPtr *publish(char *data, ssize_t length);
ContentPtr *publish(FILE *fd);

After the call the data is saved in internal buffer, and a pointer to that content is returned. The content can be removed with the ContentPtr

boolean remove(ContentPtr *cPtr);

Since the user can publish a huge amount of data, I want to add a cache management module to support

  • cache policies like LRU, FIFO, None(deny publishing when cache is full)
  • cache size control

Since I have never seen/used any library with cache management, I don't know what the best practice is to design such API. Could anyone recommend some library with good cache management API?

Some possible solutions:

  • void configPolicy(enum Policy p);
  • void configPolicy(int flags); //Use some flags and do something similar to fcntl()
  • void configSize(ssize_t size); //size=-1 if no limit
  • void configSize(string size); //supports 16M or 1G
Was it helpful?

Solution

You could take the approach that the pthreads library took by using a separate class/struct to hold the information about attributes of the thread. In your case, this extra attribute information would contain policy information, etc. So you can make your functions look like the following:

ContentPtr *publish(char *data, ssize_t length, cache_attr* attributes);
ContentPtr *publish(FILE *fd, cache_attr* attributes);

Then if the user passes a NULL value to the last argument, use whatever defaults you feel like in the underlying implementation. Otherwise, the user can pass a struct/class that describes whatever necessary attributes are needed to implement the proper policy. The nice thing about this approach is you can always have some type of abstract base class as an interface, and then create derived classes that are fed from some type of factory function if you'd like ... either way, it makes the interface flexible for both yourself and the end-user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top