Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Added key prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasff committed Dec 28, 2010
1 parent b1142fe commit 2e7e610
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 28 deletions.
4 changes: 4 additions & 0 deletions common.h
Expand Up @@ -24,6 +24,7 @@

/* options */
#define REDIS_OPT_SERIALIZER 1
#define REDIS_OPT_PREFIX 2

/* serializers */
#define REDIS_SERIALIZER_NONE 0
Expand Down Expand Up @@ -149,6 +150,9 @@ typedef struct {

int serializer;

char *prefix;
int prefix_len;

redis_mode mode;
fold_item *head;
fold_item *current;
Expand Down
24 changes: 22 additions & 2 deletions library.c
Expand Up @@ -749,8 +749,8 @@ PHPAPI RedisSock* redis_sock_create(char *host, int host_len, unsigned short por
{
RedisSock *redis_sock;

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

Expand Down Expand Up @@ -1034,6 +1034,9 @@ PHPAPI int redis_sock_write(RedisSock *redis_sock, char *cmd, size_t sz TSRMLS_D
*/
PHPAPI void redis_free_socket(RedisSock *redis_sock)
{
if(redis_sock->prefix) {
efree(redis_sock->prefix);
}
efree(redis_sock->host);
efree(redis_sock);
}
Expand Down Expand Up @@ -1140,5 +1143,22 @@ redis_unserialize(RedisSock *redis_sock, const char *val, int val_len, zval **re
}
return 0;
}

PHPAPI int
redis_key_prefix(RedisSock *redis_sock, char **key, int *key_len TSRMLS_CC) {
if(redis_sock->prefix == NULL || redis_sock->prefix_len == 0) {
return 0;
}

int ret_len = redis_sock->prefix_len + *key_len;
char *ret = ecalloc(1 + ret_len, 1);
memcpy(ret, redis_sock->prefix, redis_sock->prefix_len);
memcpy(ret + redis_sock->prefix_len, *key, *key_len);

*key = ret;
*key_len = ret_len;
return 1;
}

/* vim: set tabstop=4 softtabstop=4 noexpandtab shiftwidth=4: */

2 changes: 2 additions & 0 deletions library.h
Expand Up @@ -33,6 +33,8 @@ PHPAPI void redis_free_socket(RedisSock *redis_sock);

PHPAPI int
redis_serialize(RedisSock *redis_sock, zval *z, char **val, int *val_len TSRMLS_CC);
PHPAPI int
redis_key_prefix(RedisSock *redis_sock, char **key, int *key_len TSRMLS_CC);

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

0 comments on commit 2e7e610

Please sign in to comment.