2

I want to implement some sort of lightweight caching in Java which is easily integrable in Java and should be easy to deploy with a Java application.

The cache layer will be between the application and the database layer: no database caching, no Spring, no Hibernate, no EHcache, no http caching.

We can use a file system or a nano database so that the cache can be restored so that the cache can be restored after the process restart.

I tried LRU Cache:

http://stackoverflow.com/questions/224868/easy-simple-to-use-lru-cache-in-java

http://www.programcreek.com/2013/03/leetcode-lru-cache-java/

But I am not sure how to after overflow should I save database into database (which database will be better to use for faster insert and seek of data). Or I should use File System?

Any one has better inputs to implement caching mechanism in Java?

halfer
  • 19,824
  • 17
  • 99
  • 186
shiva
  • 730
  • 8
  • 25
  • To determine which is faster in your case, you need to measure it. The speed of a database and also the file system depends on a lot of factors. The kind of harddrive or SSD you use, the file system you use on that drive ... . So If you have tried more than one approach simply measure the speed with test data. – MrSmith42 Jun 30 '16 at 08:12
  • That's not a main concern I can use a nano database ... but question here is how to implement custom caching in java ? and which nano database will be best to use? – shiva Jun 30 '16 at 08:14
  • Even using a cache is trivial (if the cache implements the `Map` interface) implementing a persistent cache is'nt easy at all. So you want to re-invent the wheel really ? Why can't you use EHcache for example ? – PeterMmm Jun 30 '16 at 09:08
  • @PeterMmm I can use the existing java API like LRU cache or Guava cache ... but I am not sure how to save data into database (as jdbc ? it will be faster ?)as a key value pair and then then updating the value and save it again into the database ? Any ideas/algo ? – shiva Jun 30 '16 at 09:13

1 Answers1

1

But I am not sure how to after overflow should I save database into database(which database will be better to use for faster insert ans seek ok data) Or I should use File System?

It depends on the use case. If your cached values are very big, you can store each of it in a file and use the hash of the cache key as file name.

If you have values small in size, storing them as separate files would be a lot of overhead, so it is better to store the cached entries into one or a couple of files. To implement this you need to learn about "external indexes" and "memory management" or "free space management" (e.g. best fit, next fit and compaction strategies). This actually leads to the implementation of a tiny database, so may be use one :) Some stuff that comes to my mind: LevelDB, MapDB, LMDB, RocksDB

Keep in mind that caching operations come in concurrently from the application, so the cache may evict a value and a request to the same key may come in at the same time. Will you implement just the basic operations like Cache.get and Cache.put or also CAS-operations like Cache.putIfAbsent? Do you want to efficiently use multi core system, as they are common today?

Still, when using a tiny database, you will need to prepare for some months of engineering work.

Any one has better inputs to implement caching mechanism in Java?

You can read my blog at cruftex.net for some more input to implement lightweight and fast caching in Java.

For a cache implementation with overflow you can take a look at imcache. But imcache is not a fully-fledged generic cache, because for example CAS-operations are missing, see the Cache interface

My own high performance Java cache implementation cache2k, features CAS-operations, events, loaders&writers, expiry, etc. and it will eventually get some overflow to disk, too. However, I am not sure about the time frame... When you are interested to work in this area: contributions are welcome!

halfer
  • 19,824
  • 17
  • 99
  • 186
cruftex
  • 5,545
  • 2
  • 20
  • 36
  • Thanks cruftex .. definitely I will give my inputs .Yes,Definitely I need CAS-operations. I am going through the cache2K documentation , how can I integrate it with my project . – shiva Jun 30 '16 at 11:58
  • hi cruftex, I am not finding proper documentation of how to integrate cache2K with java projects? can you please help me? – shiva Jul 01 '16 at 09:33
  • https://github.com/cache2k/cache2k/blob/master/doc/src/docs/asciidoc/user-guide/sections/_start.adoc – cruftex Jul 01 '16 at 10:22
  • I am currently extending and improving the documentation. It is not officially released yet, that's why you cannot find it. See the above link for getting started. – cruftex Jul 01 '16 at 10:23
  • Hi cruftex, do we have a way to persist cache data into the tiny databases while shutdown or timeout so that we restore them while startup? either in cache2K or in in guava cache ? – shiva Jul 01 '16 at 14:40
  • No not yet. I had an almost working solution, but decided to remove it and finalize other things first. See: http://stackoverflow.com/questions/36916528/how-to-define-persistence-storage-in-cache2k and https://github.com/cache2k/cache2k/issues/30. – cruftex Jul 01 '16 at 14:55
  • If you want to have persistence and a proper cache you need to go to the more heavy (and slower) implementations for example EHCache or even Hazelcast or Infinispan. – cruftex Jul 01 '16 at 14:57
  • hi cruftex , I am really thankful to you for solving all my queries. I have gone through almost so many caching techniques like JCS, radis, memcache,guava,infinispan, hazlecast but I see infinispan and hazlecast are mainly for web applications and only ehcache has a persistent mechanism to restore data after process restart . But that is enterprise edition.I have a standalone java application in which I want to persist data to disk and retrieve it back after process restart .. then which caching should I use ? Do you have any sugestions for me? for a standalone java process? – shiva Jul 03 '16 at 20:07