This repository has been archived by the owner on Apr 8, 2023. It is now read-only.
/
hashCMD.R
77 lines (65 loc) · 1.67 KB
/
hashCMD.R
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
# This file contains functions that operate on Redis 'hash' values.
redisHGet <- function(key, field, raw=FALSE) {
if(raw){
.redisRawCmd(.raw('HGET'), .raw(key), .raw(field))
}else{
.redisCmd(.raw('HGET'), .raw(key), .raw(field))
}
}
redisHSet <- function(key, field, value, NX=FALSE) {
value <- .cerealize(value)
cmd <- 'HSET'
if(NX) cmd <- 'HSETNX'
1 == .redisCmd(.raw(cmd), .raw(key), .raw(field), value)
}
redisHMSet <- function(key, values) {
a <- c(alist(),list(.raw('HMSET')))
fieldnames <- lapply(names(values), charToRaw)
a <- c(a, Map(list, fieldnames, values, USE.NAMES=FALSE))
do.call('.redisCmd', a)
}
redisHIncrBy <- function(key, field, value)
{
.redisCmd(.raw('HINCRBY'),.raw(key),.raw(field),.raw(as.character(value)))
}
redisHExists <- function(key, field)
{
.redisCmd(.raw('HEXISTS'), .raw(key), .raw(field)) == 1
}
redisHDel <- function(key, field)
{
.redisCmd(.raw('HDEL'), .raw(key), .raw(field))
}
redisHLen <- function(key)
{
.redisCmd(.raw('HLEN'), .raw(key))
}
redisHFields <- function(key)
{
.redisCmd(.raw('HKEYS'), .raw(key))
}
redisHKeys <- function(key)
{
redisHFields(key)
}
redisHVals <- function(key)
{
.redisCmd(.raw('HVALS'), .raw(key))
}
redisHGetAll <- function(key)
{
retval <- NULL
all <- .redisCmd(.raw('HGETALL'), .raw(key))
if(!is.null(all) && length(all)>1) {
retval <- all[seq(2,length(all),by=2)]
names(retval) <- all[seq(1,length(all),by=2)]
}
retval
}
redisHMGet <- function(key, fields) {
a <- c(alist(),list(.raw('HMGET')))
a <- c(a, lapply(c(key,fields), charToRaw))
retval <- do.call('.redisCmd', a)
if(length(retval) == length(fields)) names(retval) <- fields
retval
}