7 Replies Latest reply on Jun 17, 2009 9:57 AM by nico bella

    snippet of code for eviction policy

    nico bella Newbie


      I am using Jboss cache and i having some issues configuring the eviction policy...
      I am not using the xml configuration file, i want to do it programmatically.
      The problem is, i haven't found neither in the user guide, nor in the javadoc, an example of code that explain how to configure an eviction policy...
      For instance, i want to use the expiration algorithm on a node so this one is evict 2 seconds after being created.
      I used this:

      CacheFactory cacheFactory = new DefaultCacheFactory();
       Configuration config = new Configuration();
       Cache cache = cacheFactory.createCache(config);
       Fqn fqn = Fqn.fromString("/foo/bar");
       ExpirationAlgorithmConfig eac = new ExpirationAlgorithmConfig();
       eac.setTimeToLive(2L, TimeUnit.SECONDS);
       EvictionRegionConfig erc = new EvictionRegionConfig(fqn, eac);
       Region region = cache.getRegion(fqn, true);
       EvictionConfig ec = new EvictionConfig(erc);
       Long future = new Long(System.currentTimeMillis() + 2000);
       cache.getRoot().addChild(fqn).put(ExpirationAlgorithmConfig.EXPIRATION_KEY, future);
       boolean shouldbetrue = cache.getRoot().hasChild(fqn);
       boolean shouldbefalse = cache.getRoot().hasChild(fqn);

      I used the code from the user's guide (10.4.5) but there are quite a bunch of object to set properly i guess in order for this snippet to work (ExpirationAlgorithmConfig, EvictionRegionConfig, Region, EvictionConfig...)
      I really don't know how to do this...
      Thanks for your help!