Search in sources :

Example 6 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project aerospike-client-java by aerospike.

the class Touch method runExample.

/**
	 * Demonstrate touch command.
	 */
@Override
public void runExample(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "touchkey");
    Bin bin = new Bin(params.getBinName("touchbin"), "touchvalue");
    console.info("Create record with 2 second expiration.");
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.expiration = 2;
    client.put(writePolicy, key, bin);
    console.info("Touch same record with 5 second expiration.");
    writePolicy.expiration = 5;
    Record record = client.operate(writePolicy, key, Operation.touch(), Operation.getHeader());
    if (record == null) {
        throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, null));
    }
    if (record.expiration == 0) {
        throw new Exception(String.format("Failed to get record expiration: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
    }
    console.info("Sleep 3 seconds.");
    Thread.sleep(3000);
    record = client.get(params.policy, key, bin.name);
    if (record == null) {
        throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
    }
    console.info("Success. Record still exists.");
    console.info("Sleep 4 seconds.");
    Thread.sleep(4000);
    record = client.get(params.policy, key, bin.name);
    if (record == null) {
        console.info("Success. Record expired as expected.");
    } else {
        console.error("Found record when it should have expired.");
    }
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 7 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project aerospike-client-java by aerospike.

the class TestBatch method writeRecords.

@BeforeClass
public static void writeRecords() {
    WritePolicy policy = new WritePolicy();
    policy.expiration = 2592000;
    for (int i = 1; i <= size; i++) {
        Key key = new Key(args.namespace, args.set, keyPrefix + i);
        Bin bin = new Bin(binName, valuePrefix + i);
        client.put(policy, key, bin);
    }
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) BeforeClass(org.junit.BeforeClass)

Example 8 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project aerospike-client-java by aerospike.

the class TestTouch method touch.

@Test
public void touch() {
    Key key = new Key(args.namespace, args.set, "touchkey");
    Bin bin = new Bin(args.getBinName("touchbin"), "touchvalue");
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.expiration = 2;
    client.put(writePolicy, key, bin);
    writePolicy.expiration = 5;
    Record record = client.operate(writePolicy, key, Operation.touch(), Operation.getHeader());
    assertRecordFound(key, record);
    assertNotEquals(0, record.expiration);
    Util.sleep(3000);
    record = client.get(null, key, bin.name);
    assertRecordFound(key, record);
    Util.sleep(4000);
    record = client.get(null, key, bin.name);
    assertNull(record);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) Test(org.junit.Test)

Example 9 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project XRTB by benmfaul.

the class RedissonClient method set.

/**
	 * Set a key value as string with an expiration (No expiration set on cache2k, it is already set 
	 * @param skey String. The key name.
	 * @param value String. The value.
	 * @throws Exception on aerorpike or cache errors.
	 */
public void set(String set, String skey, Object value) throws Exception {
    WritePolicy policy = new WritePolicy();
    Key key = new Key("test", set, skey);
    Bin bin1 = new Bin("value", value);
    ae.getClient().put(null, key, bin1);
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 10 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project aerospike-client-java by aerospike.

the class TestAsyncBatch method initialize.

@BeforeClass
public static void initialize() {
    sendKeys = new Key[size];
    for (int i = 0; i < size; i++) {
        sendKeys[i] = new Key(args.namespace, args.set, keyPrefix + (i + 1));
    }
    AsyncMonitor monitor = new AsyncMonitor();
    WriteHandler handler = new WriteHandler(monitor, size);
    WritePolicy policy = new WritePolicy();
    policy.expiration = 2592000;
    for (int i = 1; i <= size; i++) {
        Key key = sendKeys[i - 1];
        Bin bin = new Bin(binName, valuePrefix + i);
        client.put(eventLoop, handler, policy, key, bin);
    }
    monitor.waitTillComplete();
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) BeforeClass(org.junit.BeforeClass)

Aggregations

Bin (com.aerospike.client.Bin)21 Key (com.aerospike.client.Key)21 WritePolicy (com.aerospike.client.policy.WritePolicy)21 Record (com.aerospike.client.Record)12 Test (org.junit.Test)7 AerospikeException (com.aerospike.client.AerospikeException)6 BeforeClass (org.junit.BeforeClass)3 Policy (com.aerospike.client.policy.Policy)2 AerospikeClient (com.aerospike.client.AerospikeClient)1 Value (com.aerospike.client.Value)1 ClientPolicy (com.aerospike.client.policy.ClientPolicy)1 GenerationPolicy (com.aerospike.client.policy.GenerationPolicy)1 IndexTask (com.aerospike.client.task.IndexTask)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1