Ignite的缓存读写

Iginite的缓存方式是KV,我们先使用KV的方式进行操作.这里采用ignite官网的例子,首先定义cache内存储的对象Organization

public class Organization implements Serializable {
    @QuerySqlField(index = true)
    public Long id;
    @QuerySqlField(index = true)
    public String name;
    public Timestamp lastUpdated;

    public Organization() {
        // No-op.
    }

    public Organization(long id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override public String toString() {
        return "Person [id=" + id +
                ", name=" + name +
                ", timeNano=" + nanoTimestamp + ']';
    }
}

对Iginit Cache进行put和get操作

    String path = AffinityKeyExample.class.getResource("/example-affinitykey.xml").getFile();
    this.ignite = Ignition.start(path);
    this.cache = ignite.getOrCreateCache("organization");
    this.cache.clear();
    this.orgs.add(new Organization(1L,"CNY"));
    this.orgs.add(new Organization(2L,"USD"));
    this.orgs.stream().forEach(x ->{
        this.cache.put(x.id,x);
    });
    this.cache.query(new ScanQuery()).getAll().stream().forEach(x->{
        logger.info(x.toString());
    });

查询

扫描查询

        Query> qry = new ScanQuery(
                (i, p) -> p.name().contains("CNY"));
        cache.query(qry).getAll().stream().forEach(x->{
            logger.info("CNY is matched");
        });

索引查询

KV扫描查询快,还是SQL查询快?

BinaryObject

Organization对象进入缓存时会进行序列话和反序列化操作,使用BinaryObject操作,可以不使用默认的序列化操作.
使用BinaryObject可以减少大型对象仅对少量字段进行读写的时间

    String path = AffinityKeyExample.class.getResource("/example-affinitykey.xml").getFile();
    this.ignite = Ignition.start(path);
    this.binary = this.ignite.binary();
    this.cache = ignite.getOrCreateCache("organization").withKeepBinary();
    BinaryObject val = binary.builder("com.goxplanet.Organization")
            .setField("id", 1L, Long.class)
            .setField("name", "USD", String.class)
            .build();
    this.cache.put(id, val);
    BinaryObject value = this.cache.get(id);
    BinaryField nameFiled = value.type().field("name");
    logger.info(nameFiled.value(value));    

持续查询

  • Cache Interceptor
  • Entry Processor
  • Continuous Query

你可能感兴趣的:(Ignite的缓存读写)