# Set ttl with UDF

**URL:** <https://discuss.aerospike.com/t/set-ttl-with-udf/228>\
**Category:** User Defined Functions (UDF)\
**Created:** [August 16, 2014, 12:34am UTC](https://discuss.aerospike.com/t/set-ttl-with-udf/228 "2014-08-16T00:34:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wchu](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@wchu](https://discuss.aerospike.com/u/wchu)\
**Post date:** [August 16, 2014, 12:34am UTC](https://discuss.aerospike.com/t/set-ttl-with-udf/228/1 "2014-08-16T00:34:46Z")

</div>

by saterenko » Tue Jun 10, 2014 4:13 am

Is there way to set ttl with UDF script? I need to create record with ttl 60 sec, not default from config file.

---

<div class="post-metadata">

**Author:** ![wchu](https://avatars.discourse-cdn.com/v4/letter/w/a5b964/32.png) [@wchu](https://discuss.aerospike.com/u/wchu)\
**Post date:** [August 16, 2014, 12:36am UTC](https://discuss.aerospike.com/t/set-ttl-with-udf/228/2 "2014-08-16T00:36:38Z")

</div>

Yes, this feature is added in Aerospike Server 3.3.12

---

<div class="post-metadata">

**Author:** ![ishika](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@ishika](https://discuss.aerospike.com/u/ishika)\
**Post date:** [September 4, 2014, 1:31am UTC](https://discuss.aerospike.com/t/set-ttl-with-udf/228/3 "2014-09-04T01:31:55Z")

</div>

Hi wchu,

I am attempting to set the ttyl via the java api without success. Was wondering if you can help me. I verified that my server is 3.3.12.

Here is my client code (it is using aerospike java api: 3.0.22):

```
public Object callUserDefinedFunction(WritePolicy policy,
                                      Key key,
                                      Value[] list,
                                      String luaName,
                                      String functionName)
                                      throws AerospikeException {
                mLog.info("EXPIRATION " + policy.expiration);
                return mAero.execute(policy, key, luaName,
                            functionName, list);
}

```

I log the expiration and its set properly: 2014-09-03 17:30:10 INFO UserStoreService:141 - EXPIRATION 5

The above function successfully calls my udf on the server and writes to the proper location, however it does not honor the expiration. I noticed that the execute method takes in a generic Policy type which does not have an expiration param. [http://grepcode.com/file/repo1.maven.org/maven2/com.aerospike/aerospike-client/3.0.22/com/aerospike/client/AerospikeClient.java?av=f#781](http://grepcode.com/file/repo1.maven.org/maven2/com.aerospike/aerospike-client/3.0.22/com/aerospike/client/AerospikeClient.java?av=f#781).

thank you for your help

---

<div class="post-metadata">

**Author:** ![brad](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/brad/32/2566_2.png) [@brad](https://discuss.aerospike.com/u/brad)\
**Post date:** [September 5, 2014, 5:13pm UTC](https://discuss.aerospike.com/t/set-ttl-with-udf/228/4 "2014-09-05T17:13:21Z")

</div>

Hi,

To set the ttl in a record, use the record.set\_ttl() function in your UDF. The syntax is

record.set\_ttl(rec, ttl)

where the ttl is in seconds. Use this function before updating or creating the record.

Thanks,

-Brad

---

<div class="post-metadata">

**Author:** ![ishika](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@ishika](https://discuss.aerospike.com/u/ishika)\
**Post date:** [September 5, 2014, 7:10pm UTC](https://discuss.aerospike.com/t/set-ttl-with-udf/228/5 "2014-09-05T19:10:55Z")

</div>

thanks! This is what I ended up using
