// 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 java.util.Map.Entry;

import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

public class TestFunctional {
    private static void test(RMap<String,Integer> cache) {
        // basic put, get and remove
        cache.put("v", 123);
        System.out.println(cache.get("v"));
        cache.remove("v");
        System.out.println(cache.get("v"));
        cache.put("v", 123);
        System.out.println(cache.get("v"));
        // persistence
        Integer counter = cache.get("counter");
        if(counter == null) {
            counter = 0;
        }
        counter++;
        System.out.println(counter);
        cache.put("counter", counter);
        // list all
        for(Entry<String,Integer> en : cache.entrySet()) {
            System.out.printf("%s = %d\n", en.getKey(), en.getValue());
        }
        // listener does not seem to work as I expect in 3.13.6
    }
    public static void main(String[] args) throws Exception {
        ((Logger)LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(Level.WARN); // hack to avoid debug & info output
        Config cfg = new Config();
        //cfg.useClusterServers().addNodeAddress("redis://localhost:6379");
        cfg.useSingleServer().setAddress("redis://localhost:6379");
        RedissonClient client = Redisson.create(cfg);
        RMap<String,Integer> cache = client.getMap("functional_cache");
        test(cache);
        client.shutdown();
    }
}
