// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package cache.redis;

import redis.clients.jedis.Jedis;

public class TestFunctional {
    private static void test(Jedis cache) {
        // basic put, get and remove
        cache.set("v", Integer.toString(123));
        System.out.println(cache.get("v"));
        cache.del("v");
        System.out.println(cache.get("v") != null ? Integer.parseInt(cache.get("v")) : null);
        cache.set("v", Integer.toString(123));
        System.out.println(cache.get("v"));
        // persistence
        int counter;
        if(cache.get("counter") == null) {
            counter = 0;
        } else {
            counter = Integer.parseInt(cache.get("counter"));
        }
        counter++;
        System.out.println(counter);
        cache.set("counter", Integer.toString(counter));
        // list all
        for(String key : cache.keys("*")) {
            System.out.printf("%s = %d\n", key, Integer.parseInt(cache.get(key)));
        }
        // listener not supported
    }
    private static final int FUNCTIONAL_CACHE = 0;
    public static void main(String[] args) throws Exception {
        Jedis cache = new Jedis("localhost", 6379);
        cache.select(FUNCTIONAL_CACHE);
        test(cache);
        cache.close();
    }
}
