Ignite 的使用过程(二) 设置过期时间

Ignite 的使用过程(二) 设置过期时间

官网地址

3.11.过期策略
https://www.zybuluo.com/liyuj/note/964191#322ignitecache

代码测试

import org.apache.ignite.Ignite;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.query.SqlQuery;
import org.apache.ignite.configuration.CacheConfiguration;

import javax.cache.Cache;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.expiry.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhanghaichao on 2018/5/18.
 */
public class testignite {
    public static void main(String[] args) {
        Ignition.setClientMode(true);
        Ignition.start("D:\\apache-ignite-fabric-2.4.0-bin\\examples\\config\\example-cache.xml");
        Ignite ignite = Ignition.ignite(); // 当ignite为客户端方式时,启动时会等待服务端启动和连接成功后才会向下继续

        CacheConfiguration cacheOnlyPersonCfg = new CacheConfiguration<>("personCache");

        //指定缓存的过期时间
//        cacheOnlyPersonCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 5)));
        cacheOnlyPersonCfg.setIndexedTypes(String.class, Person.class); // 注意配置注册key和value是为了sql查询
        IgniteCache cache = ignite.getOrCreateCache(cacheOnlyPersonCfg).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5)));


        Person ps = new Person();
        ps.setId(1);
        ps.setName("aa11");
        ps.setCity_id(1);
        cache.put("aaaaa", ps);

        Person ps2 = new Person();
        ps2.setId(2);
        ps2.setName("aa22");
        ps2.setCity_id(1);
        cache.put("bbbbbb", ps2);

        try {
            Thread.sleep(2000);
            SqlQuery sql = new SqlQuery(Person.class, "1=1");
            List> cursor = cache.query(sql).getAll();
            if (cursor != null) {
                for (Cache.Entry p : cursor) {
                    System.out.println("延时两秒查询结果::" + p.getValue().getName());
                }
            }

            Person person1 = cache.get("bbbbbb");
            System.out.println("延时两秒查询结果根据key进行查询的结果::" + person1.getName());

        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            Thread.sleep(7000);
            SqlQuery sql = new SqlQuery(Person.class, "1=1");
            List> cursor = cache.query(sql).getAll();
            if (cursor != null) {
                for (Cache.Entry p : cursor) {
                    System.out.println("延时七秒查询结果::" + p.getValue().getName());
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束");
    }
}

备注

  1. ignite设置了两种过期时间方式
  2. 第一种在创建动态缓存的时候,使用

     CacheConfiguration cacheOnlyPersonCfg = new CacheConfiguration<>("personCache");
     cacheOnlyPersonCfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.SECONDS, 5)));
    

    来指定personCache的统一过期时间

3.在未统一设置过期时间的情况下,可以使用单独设置每一次的过期时间来处理:

IgniteCache cache = ignite.getOrCreateCache(cacheOnlyPersonCfg).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 15)));

这样就相当于设置了每次操作ignite操作的时候设置过期时间位15秒

注意!!不能在

    IgniteCache cache = ignite.getOrCreateCache(cacheOnlyPersonCfg);

后再设置

    cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 15)));

withExpiryPolicy方法是用来返回一个IgniteCache的而不是设置上一个IgniteCache的

你可能感兴趣的:(java,Ignite)