forked from danielealbano/cachegrand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_redis_command_psetex.c
83 lines (74 loc) · 2.81 KB
/
module_redis_command_psetex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Copyright (C) 2018-2022 Vito Castellano
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
**/
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <strings.h>
#include <arpa/inet.h>
#include <assert.h>
#include "misc.h"
#include "exttypes.h"
#include "log/log.h"
#include "clock.h"
#include "spinlock.h"
#include "xalloc.h"
#include "data_structures/small_circular_queue/small_circular_queue.h"
#include "data_structures/double_linked_list/double_linked_list.h"
#include "data_structures/queue_mpmc/queue_mpmc.h"
#include "slab_allocator.h"
#include "data_structures/hashtable/mcmp/hashtable.h"
#include "data_structures/hashtable/mcmp/hashtable_op_set.h"
#include "data_structures/hashtable/spsc/hashtable_spsc.h"
#include "protocol/redis/protocol_redis.h"
#include "protocol/redis/protocol_redis_reader.h"
#include "protocol/redis/protocol_redis_writer.h"
#include "module/module.h"
#include "network/io/network_io_common.h"
#include "config.h"
#include "fiber.h"
#include "network/channel/network_channel.h"
#include "storage/io/storage_io_common.h"
#include "storage/channel/storage_channel.h"
#include "storage/db/storage_db.h"
#include "module/redis/module_redis.h"
#include "module/redis/module_redis_connection.h"
#include "module/redis/module_redis_command.h"
#include "network/network.h"
#include "worker/worker_stats.h"
#include "worker/worker_context.h"
#define TAG "module_redis_command_psetex"
MODULE_REDIS_COMMAND_FUNCPTR_COMMAND_END(psetex) {
bool return_res = false;
bool key_and_value_owned = false;
module_redis_command_psetex_context_t *context = connection_context->command.context;
storage_db_expiry_time_ms_t expiry_time_ms =
clock_realtime_coarse_int64_ms() + (int64_t)context->milliseconds.value;
if (unlikely(!storage_db_op_set(
connection_context->db,
context->key.value.key,
context->key.value.length,
context->value.value.chunk_sequence,
expiry_time_ms))) {
return_res = module_redis_connection_error_message_printf_noncritical(
connection_context,
"ERR psetex failed");
goto end;
}
key_and_value_owned = true;
return_res = module_redis_connection_send_ok(connection_context);
end:
if (likely(key_and_value_owned)) {
// Mark both the key and the chunk_sequence as NULL as the storage db now owns them, we don't want them to be
// automatically freed at the end of the execution, especially the key as the hashtable might not need to hold
// a reference to it, it might have already been freed
context->key.value.key = NULL;
context->value.value.chunk_sequence = NULL;
}
return return_res;
}