Skip to content

Commit 2e7e610

Browse files
committed
Added key prefix.
1 parent b1142fe commit 2e7e610

File tree

4 files changed

+199
-28
lines changed

4 files changed

+199
-28
lines changed

common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
/* options */
2626
#define REDIS_OPT_SERIALIZER 1
27+
#define REDIS_OPT_PREFIX 2
2728

2829
/* serializers */
2930
#define REDIS_SERIALIZER_NONE 0
@@ -149,6 +150,9 @@ typedef struct {
149150

150151
int serializer;
151152

153+
char *prefix;
154+
int prefix_len;
155+
152156
redis_mode mode;
153157
fold_item *head;
154158
fold_item *current;

library.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,8 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
749749
{
750750
RedisSock *redis_sock;
751751

752-
redis_sock = emalloc(sizeof *redis_sock);
753-
redis_sock->host = emalloc(host_len + 1);
752+
redis_sock = ecalloc(1, sizeof(RedisSock));
753+
redis_sock->host = ecalloc(host_len + 1, 1);
754754
redis_sock->stream = NULL;
755755
redis_sock->status = REDIS_SOCK_STATUS_DISCONNECTED;
756756

@@ -1034,6 +1034,9 @@ PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz TSRMLS_D
10341034
*/
10351035
PHPAPI void redis_free_socket(RedisSock *redis_sock)
10361036
{
1037+
if(redis_sock->prefix) {
1038+
efree(redis_sock->prefix);
1039+
}
10371040
efree(redis_sock->host);
10381041
efree(redis_sock);
10391042
}
@@ -1140,5 +1143,22 @@ redis_unserialize(RedisSock *redis_sock, const char *val, int val_len, zval **re
11401143
}
11411144
return 0;
11421145
}
1146+
1147+
PHPAPI int
1148+
redis_key_prefix(RedisSock *redis_sock, char **key, int *key_len TSRMLS_CC) {
1149+
if(redis_sock->prefix == NULL || redis_sock->prefix_len == 0) {
1150+
return 0;
1151+
}
1152+
1153+
int ret_len = redis_sock->prefix_len + *key_len;
1154+
char *ret = ecalloc(1 + ret_len, 1);
1155+
memcpy(ret, redis_sock->prefix, redis_sock->prefix_len);
1156+
memcpy(ret + redis_sock->prefix_len, *key, *key_len);
1157+
1158+
*key = ret;
1159+
*key_len = ret_len;
1160+
return 1;
1161+
}
1162+
11431163
/* vim: set tabstop=4 softtabstop=4 noexpandtab shiftwidth=4: */
11441164

library.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ PHPAPI void redis_free_socket(RedisSock *redis_sock);
3333

3434
PHPAPI int
3535
redis_serialize(RedisSock *redis_sock, zval *z, char **val, int *val_len TSRMLS_CC);
36+
PHPAPI int
37+
redis_key_prefix(RedisSock *redis_sock, char **key, int *key_len TSRMLS_CC);
3638

3739
PHPAPI int
3840
redis_unserialize(RedisSock *redis_sock, const char *val, int val_len, zval **return_value TSRMLS_CC);

0 commit comments

Comments
 (0)