JCache is a little project I’ve been developing to make small persistant databases for my apps.
A number of the ruby programs I write are “glue programs” - they’re small and they sit at the end of (or between) larger programmes, providing an outlet (or pipe) for data. Sometimes those large programs are running, and sometimes they aren’t.
In addition, it seems like people are getting more and more into the whole “personal quantification” thing. I’m not going about it quite as rigorously as some (I’m not getting a pedometer any time in the near future, don’t worry), but it’s still handy to be able to record things day-to-day. Since I’m doing a lot of work in the terminal, a lot of the time the fastest way of getting information recorded is through a tiny shell script (or quicksilver plugin).
There’s an obvious need for a small, centralised database here. Since I often want to hack the data outside of whatever program is reading it, it’s nice to have it stored plaintext1. The solution is jcache - an interface to a number of yaml text files stored in my home directory. It’s small, fast, completely ruby, unobtrusive, and adaptable.
I haven’t published this to rubygems or anything, so to install you’ll have to grab it from git and install it yourself:
1 2 3 4 5 6 7 8
> git clone https://github.com/jyruzicka/jcache Cloning into 'jcache'... remote: Counting objects: 86, done. remote: Compressing objects: 100% (30/30), done. remote: Total 86 (delta 33), reused 85 (delta 32) Unpacking objects: 100% (86/86), done. > cd jcache > gem build jcache.gemspec && gem install jcache-1.0.1.gem
(Note: version number may differ when you run this.)
JCache is now ready to run.
Using in ruby
Simply require it, and load/create your cache:
1 2 3 4 5 6 7 8
require 'jcache' cache = JCache::Cache['foo'] # loading the cache "foo" cache[:key] = 3 # set a value cache.save # save to file another_cache = JCache::Cache['foo'] # load it again another_cache[:key] # => 3
Using it on the command line
cache to see which caches exist on your computer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
> cache Current JSON caches: * foo * med * nt * of-cache * of2do * pkfetch * projbox * simpled * slock * stoit-old * stoit * tcount
cache <cachename> to view a given cache:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
> cache slock Contents of cache slock: --- :times: :weekdays: '08:45': You should think about starting work soon... '09:00': It's about time to start work now. '13:00': Back to work. '15:00': Have a break! Afternoon tea time '17:00': End of the work day. :always: '10:30': Have a break! Morning tea time. '12:00': Have a break! Lunch time. '21:00': Do you need to do anything before you go to bed? '22:00': Bed time. :last_accessed: 2013-04-09 13:10:06.044186000 +12:00
Yes, there’s plenty of reasons why you’d want to chuck it in sqlite or similar, but at the data sizes we’re talking about (5-50 records), plaintext is perfectly fine. And if I want something larger, well, I’d probably end up making something specially for that application. ↩