forked from ServiceStack/ServiceStack.Redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisClientHash.cs
More file actions
162 lines (138 loc) · 3.14 KB
/
RedisClientHash.cs
File metadata and controls
162 lines (138 loc) · 3.14 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// 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 ServiceStack, Inc. All Rights Reserved.
//
// Licensed under the same terms of ServiceStack.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ServiceStack.Redis
{
/// <summary>
/// Wrap the common redis set operations under a ICollection[string] interface.
/// </summary>
internal partial class RedisClientHash
: IRedisHash
{
private readonly RedisClient client;
private readonly string hashId;
public RedisClientHash(RedisClient client, string hashId)
{
this.client = client;
this.hashId = hashId;
}
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return client.GetAllEntriesFromHash(hashId).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(KeyValuePair<string, string> item)
{
client.SetEntryInHash(hashId, item.Key, item.Value);
}
public bool AddIfNotExists(KeyValuePair<string, string> item)
{
return client.SetEntryInHashIfNotExists(hashId, item.Key, item.Value);
}
public void AddRange(IEnumerable<KeyValuePair<string, string>> items)
{
client.SetRangeInHash(hashId, items);
}
public long IncrementValue(string key, int incrementBy)
{
return client.IncrementValueInHash(hashId, key, incrementBy);
}
public void Clear()
{
client.Remove(hashId);
}
public bool Contains(KeyValuePair<string, string> item)
{
var itemValue = client.GetValueFromHash(hashId, item.Key);
return itemValue == item.Value;
}
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
{
var allItemsInHash = client.GetAllEntriesFromHash(hashId);
var i = arrayIndex;
foreach (var item in allItemsInHash)
{
if (i >= array.Length) return;
array[i++] = item;
}
}
public bool Remove(KeyValuePair<string, string> item)
{
if (Contains(item))
{
client.RemoveEntryFromHash(hashId, item.Key);
return true;
}
return false;
}
public int Count
{
get { return (int)client.GetHashCount(hashId); }
}
public bool IsReadOnly
{
get { return false; }
}
public bool ContainsKey(string key)
{
return client.HashContainsEntry(hashId, key);
}
public void Add(string key, string value)
{
client.SetEntryInHash(hashId, key, value);
}
public bool Remove(string key)
{
return client.RemoveEntryFromHash(hashId, key);
}
public bool TryGetValue(string key, out string value)
{
value = client.GetValueFromHash(hashId, key);
return value != null;
}
public string this[string key]
{
get
{
return client.GetValueFromHash(hashId, key);
}
set
{
client.SetEntryInHash(hashId, key, value);
}
}
public ICollection<string> Keys
{
get
{
return client.GetHashKeys(hashId);
}
}
public ICollection<string> Values
{
get
{
return client.GetHashValues(hashId);
}
}
public string Id
{
get { return hashId; }
}
}
}