forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisClient_Hash.cs
More file actions
140 lines (118 loc) · 4.21 KB
/
RedisClient_Hash.cs
File metadata and controls
140 lines (118 loc) · 4.21 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//
// https://github.com/ServiceStack/ServiceStack.Redis
// ServiceStack.Redis: ECMA CLI Binding to the Redis key-value storage system
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2013 Service Stack LLC. All Rights Reserved.
//
// Licensed under the same terms of ServiceStack.
//
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Model;
using ServiceStack.Text;
namespace ServiceStack.Redis
{
public partial class RedisClient
: IRedisClient
{
public IHasNamed<IRedisHash> Hashes { get; set; }
internal class RedisClientHashes
: IHasNamed<IRedisHash>
{
private readonly RedisClient client;
public RedisClientHashes(RedisClient client)
{
this.client = client;
}
public IRedisHash this[string hashId]
{
get
{
return new RedisClientHash(client, hashId);
}
set
{
var hash = this[hashId];
hash.Clear();
hash.CopyTo(value.ToArray(), 0);
}
}
}
public bool SetEntryInHash(string hashId, string key, string value)
{
return base.HSet(hashId, key.ToUtf8Bytes(), value.ToUtf8Bytes()) == Success;
}
public bool SetEntryInHashIfNotExists(string hashId, string key, string value)
{
return base.HSetNX(hashId, key.ToUtf8Bytes(), value.ToUtf8Bytes()) == Success;
}
public void SetRangeInHash(string hashId, IEnumerable<KeyValuePair<string, string>> keyValuePairs)
{
var keyValuePairsList = keyValuePairs.ToList();
if (keyValuePairsList.Count == 0) return;
var keys = new byte[keyValuePairsList.Count][];
var values = new byte[keyValuePairsList.Count][];
for (var i = 0; i < keyValuePairsList.Count; i++)
{
var kvp = keyValuePairsList[i];
keys[i] = kvp.Key.ToUtf8Bytes();
values[i] = kvp.Value.ToUtf8Bytes();
}
base.HMSet(hashId, keys, values);
}
public long IncrementValueInHash(string hashId, string key, int incrementBy)
{
return base.HIncrby(hashId, key.ToUtf8Bytes(), incrementBy);
}
public long IncrementValueInHash(string hashId, string key, long incrementBy)
{
return base.HIncrby(hashId, key.ToUtf8Bytes(), incrementBy);
}
public double IncrementValueInHash(string hashId, string key, double incrementBy)
{
return base.HIncrbyFloat(hashId, key.ToUtf8Bytes(), incrementBy);
}
public string GetValueFromHash(string hashId, string key)
{
return base.HGet(hashId, key.ToUtf8Bytes()).FromUtf8Bytes();
}
public bool HashContainsEntry(string hashId, string key)
{
return base.HExists(hashId, key.ToUtf8Bytes()) == Success;
}
public bool RemoveEntryFromHash(string hashId, string key)
{
return base.HDel(hashId, key.ToUtf8Bytes()) == Success;
}
public long GetHashCount(string hashId)
{
return base.HLen(hashId);
}
public List<string> GetHashKeys(string hashId)
{
var multiDataList = base.HKeys(hashId);
return multiDataList.ToStringList();
}
public List<string> GetHashValues(string hashId)
{
var multiDataList = base.HVals(hashId);
return multiDataList.ToStringList();
}
public Dictionary<string, string> GetAllEntriesFromHash(string hashId)
{
var multiDataList = base.HGetAll(hashId);
return multiDataList.ToStringDictionary();
}
public List<string> GetValuesFromHash(string hashId, params string[] keys)
{
if (keys.Length == 0) return new List<string>();
var keyBytes = ConvertToBytes(keys);
var multiDataList = base.HMGet(hashId, keyBytes);
return multiDataList.ToStringList();
}
}
}