Skip to content

Commit b2efc30

Browse files
committed
Implement map.set_ttl
1 parent 4cc593e commit b2efc30

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

hazelcast/protocol/codec/map_message_type.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@
5555
MAP_VALUESWITHPAGINGPREDICATE = 0x0139
5656
MAP_ENTRIESWITHPAGINGPREDICATE = 0x013a
5757
MAP_CLEARNEARCACHE = 0x013b
58+
MAP_SETTTL = 0x0149
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from hazelcast.serialization.bits import *
2+
from hazelcast.protocol.client_message import ClientMessage
3+
from hazelcast.protocol.custom_codec import *
4+
from hazelcast.util import ImmutableLazyDataList
5+
from hazelcast.protocol.codec.map_message_type import *
6+
7+
REQUEST_TYPE = MAP_SETTTL
8+
RESPONSE_TYPE = 101
9+
RETRYABLE = False
10+
11+
12+
def calculate_size(name, key, ttl):
13+
""" Calculates the request payload size"""
14+
data_size = 0
15+
data_size += calculate_size_str(name)
16+
data_size += calculate_size_data(key)
17+
data_size += LONG_SIZE_IN_BYTES
18+
return data_size
19+
20+
21+
def encode_request(name, key, ttl):
22+
""" Encode request into client_message"""
23+
client_message = ClientMessage(payload_size=calculate_size(name, key, ttl))
24+
client_message.set_message_type(REQUEST_TYPE)
25+
client_message.set_retryable(RETRYABLE)
26+
client_message.append_str(name)
27+
client_message.append_data(key)
28+
client_message.append_long(ttl)
29+
client_message.update_frame_length()
30+
return client_message
31+
32+
33+
def decode_response(client_message, to_object=None):
34+
""" Decode response from client message"""
35+
parameters = dict(response=None)
36+
parameters['response'] = client_message.read_bool()
37+
return parameters
38+
39+
40+

hazelcast/proxy/map.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
map_is_locked_codec, map_key_set_codec, map_key_set_with_predicate_codec, map_load_all_codec, \
1111
map_load_given_keys_codec, map_lock_codec, map_put_codec, map_put_all_codec, map_put_if_absent_codec, \
1212
map_put_transient_codec, map_size_codec, map_remove_codec, map_remove_if_same_codec, \
13-
map_remove_entry_listener_codec, map_replace_codec, map_replace_if_same_codec, map_set_codec, map_try_lock_codec, \
14-
map_try_put_codec, map_try_remove_codec, map_unlock_codec, map_values_codec, map_values_with_predicate_codec, \
15-
map_add_interceptor_codec, map_execute_on_all_keys_codec, map_execute_on_key_codec, map_execute_on_keys_codec, \
16-
map_execute_with_predicate_codec, map_add_near_cache_entry_listener_codec
13+
map_remove_entry_listener_codec, map_replace_codec, map_replace_if_same_codec, map_set_codec, map_set_ttl_codec, \
14+
map_try_lock_codec, map_try_put_codec, map_try_remove_codec, map_unlock_codec, map_values_codec, \
15+
map_values_with_predicate_codec, map_add_interceptor_codec, map_execute_on_all_keys_codec, map_execute_on_key_codec, \
16+
map_execute_on_keys_codec, map_execute_with_predicate_codec, map_add_near_cache_entry_listener_codec
1717
from hazelcast.proxy.base import Proxy, EntryEvent, EntryEventType, get_entry_listener_flags, MAX_SIZE
1818
from hazelcast.util import check_not_none, thread_id, to_millis
1919
from hazelcast import six
@@ -725,6 +725,22 @@ def set(self, key, value, ttl=-1):
725725
value_data = self._to_data(value)
726726
return self._set_internal(key_data, value_data, ttl)
727727

728+
def set_ttl(self, key, ttl):
729+
"""
730+
Updates the TTL (time to live) value of the entry specified by the given key with a new TTL value. New TTL
731+
value is valid starting from the time this operation is invoked, not since the time the entry was created.
732+
If the entry does not exist or is already expired, this call has no effect.
733+
734+
:param key: (object), the key of the map entry.
735+
:param ttl: (int), maximum time for this entry to stay in the map (0 means infinite,
736+
negative means map config default)
737+
"""
738+
check_not_none(key, "key can't be None")
739+
check_not_none(ttl, "ttl can't be None")
740+
key_data = self._to_data(key)
741+
return self._encode_invoke_on_key(map_set_ttl_codec, key_data, key=key_data, ttl=to_millis(ttl))
742+
743+
728744
def size(self):
729745
"""
730746
Returns the number of entries in this map.

tests/proxy/map_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,15 @@ def test_set(self):
437437
self.map.set("key", "value")
438438

439439
self.assertEqual(self.map.get("key"), "value")
440+
441+
def test_set_ttl(self):
442+
self.map.put("key", "value")
443+
self.map.set_ttl("key", 0.1)
444+
445+
def evicted():
446+
self.assertFalse(self.map.contains_key("key"))
447+
448+
self.assertTrueEventually(evicted, 1)
440449

441450
def test_size(self):
442451
self._fill_map()

0 commit comments

Comments
 (0)