1616
1717namespace ServiceStack . Redis . Generic
1818{
19- /// <summary>
20- /// Wrap the common redis set operations under a ICollection[string] interface.
21- /// </summary>
22- internal class RedisClientHash < TKey , T >
23- : IRedisHash < TKey , T >
24- {
25- private readonly RedisTypedClient < T > client ;
26- private readonly string hashId ;
27-
28- public RedisClientHash ( RedisTypedClient < T > client , string hashId )
29- {
30- this . client = client ;
31- this . hashId = hashId ;
32- }
33-
34- public string Id
35- {
36- get { return this . hashId ; }
37- }
38-
39- public IEnumerator < KeyValuePair < TKey , T > > GetEnumerator ( )
40- {
41- return client . GetAllEntriesFromHash ( this ) . GetEnumerator ( ) ;
42- }
43-
44- IEnumerator IEnumerable . GetEnumerator ( )
45- {
46- return GetEnumerator ( ) ;
47- }
48-
49- public Dictionary < TKey , T > GetAll ( )
50- {
51- return client . GetAllEntriesFromHash ( this ) ;
52- }
53-
54- public void Add ( KeyValuePair < TKey , T > item )
55- {
56- client . SetEntryInHash ( this , item . Key , item . Value ) ;
57- }
58-
59- public void Clear ( )
60- {
61- client . RemoveEntry ( this ) ;
62- }
63-
64- public bool Contains ( KeyValuePair < TKey , T > item )
65- {
66- var value = client . GetValueFromHash ( this , item . Key ) ;
67- return ! Equals ( value , default ( T ) ) && Equals ( value , item . Value ) ;
68- }
69-
70- public void CopyTo ( KeyValuePair < TKey , T > [ ] array , int arrayIndex )
71- {
72- var allItemsInHash = client . GetAllEntriesFromHash ( this ) ;
73-
74- var i = arrayIndex ;
75- foreach ( var entry in allItemsInHash )
76- {
77- if ( i >= array . Length ) return ;
78- array [ i ] = entry ;
79- }
80- }
81-
82- public bool Remove ( KeyValuePair < TKey , T > item )
83- {
84- return Contains ( item ) && client . RemoveEntryFromHash ( this , item . Key ) ;
85- }
86-
87- public int Count
88- {
89- get { return ( int ) client . GetHashCount ( this ) ; }
90- }
91-
92- public bool IsReadOnly
93- {
94- get { return false ; }
95- }
96-
97- public bool ContainsKey ( TKey key )
98- {
99- return client . HashContainsEntry ( this , key ) ;
100- }
101-
102- public void Add ( TKey key , T value )
103- {
104- client . SetEntryInHash ( this , key , value ) ;
105- }
106-
107- public bool Remove ( TKey key )
108- {
109- return client . RemoveEntryFromHash ( this , key ) ;
110- }
111-
112- public bool TryGetValue ( TKey key , out T value )
113- {
114- if ( ContainsKey ( key ) )
115- {
116- value = client . GetValueFromHash ( this , key ) ;
117- return true ;
118- }
119- value = default ( T ) ;
120- return false ;
121- }
122-
123- public T this [ TKey key ]
124- {
125- get { return client . GetValueFromHash ( this , key ) ; }
126- set { client . SetEntryInHash ( this , key , value ) ; }
127- }
128-
129- public ICollection < TKey > Keys
130- {
131- get { return client . GetHashKeys ( this ) ; }
132- }
133-
134- public ICollection < T > Values
135- {
136- get { return client . GetHashValues ( this ) ; }
137- }
138-
139- public List < TKey > GetAllKeys ( )
140- {
141- return client . GetHashKeys ( this ) ;
142- }
143-
144- public List < T > GetAllValues ( )
145- {
146- return client . GetHashValues ( this ) ;
147- }
148- }
19+ /// <summary>
20+ /// Wrap the common redis set operations under a ICollection[string] interface.
21+ /// </summary>
22+ internal class RedisClientHash < TKey , T >
23+ : IRedisHash < TKey , T >
24+ {
25+ private readonly RedisTypedClient < T > client ;
26+ private readonly string hashId ;
27+
28+ public RedisClientHash ( RedisTypedClient < T > client , string hashId )
29+ {
30+ this . client = client ;
31+ this . hashId = hashId ;
32+ }
33+
34+ public string Id
35+ {
36+ get { return this . hashId ; }
37+ }
38+
39+ public IEnumerator < KeyValuePair < TKey , T > > GetEnumerator ( )
40+ {
41+ return client . GetAllEntriesFromHash ( this ) . GetEnumerator ( ) ;
42+ }
43+
44+ IEnumerator IEnumerable . GetEnumerator ( )
45+ {
46+ return GetEnumerator ( ) ;
47+ }
48+
49+ public Dictionary < TKey , T > GetAll ( )
50+ {
51+ return client . GetAllEntriesFromHash ( this ) ;
52+ }
53+
54+ public void Add ( KeyValuePair < TKey , T > item )
55+ {
56+ client . SetEntryInHash ( this , item . Key , item . Value ) ;
57+ }
58+
59+ public void Clear ( )
60+ {
61+ client . RemoveEntry ( this ) ;
62+ }
63+
64+ public bool Contains ( KeyValuePair < TKey , T > item )
65+ {
66+ var value = client . GetValueFromHash ( this , item . Key ) ;
67+ return ! Equals ( value , default ( T ) ) && Equals ( value , item . Value ) ;
68+ }
69+
70+ public void CopyTo ( KeyValuePair < TKey , T > [ ] array , int arrayIndex )
71+ {
72+ var allItemsInHash = client . GetAllEntriesFromHash ( this ) ;
73+
74+ var i = arrayIndex ;
75+ foreach ( var entry in allItemsInHash )
76+ {
77+ if ( i >= array . Length ) return ;
78+ array [ i ] = entry ;
79+ }
80+ }
81+
82+ public bool Remove ( KeyValuePair < TKey , T > item )
83+ {
84+ return Contains ( item ) && client . RemoveEntryFromHash ( this , item . Key ) ;
85+ }
86+
87+ public int Count
88+ {
89+ get { return ( int ) client . GetHashCount ( this ) ; }
90+ }
91+
92+ public bool IsReadOnly
93+ {
94+ get { return false ; }
95+ }
96+
97+ public bool ContainsKey ( TKey key )
98+ {
99+ return client . HashContainsEntry ( this , key ) ;
100+ }
101+
102+ public void Add ( TKey key , T value )
103+ {
104+ client . SetEntryInHash ( this , key , value ) ;
105+ }
106+
107+ public bool Remove ( TKey key )
108+ {
109+ return client . RemoveEntryFromHash ( this , key ) ;
110+ }
111+
112+ public bool TryGetValue ( TKey key , out T value )
113+ {
114+ if ( ContainsKey ( key ) )
115+ {
116+ value = client . GetValueFromHash ( this , key ) ;
117+ return true ;
118+ }
119+ value = default ( T ) ;
120+ return false ;
121+ }
122+
123+ public T this [ TKey key ]
124+ {
125+ get { return client . GetValueFromHash ( this , key ) ; }
126+ set { client . SetEntryInHash ( this , key , value ) ; }
127+ }
128+
129+ public ICollection < TKey > Keys
130+ {
131+ get { return client . GetHashKeys ( this ) ; }
132+ }
133+
134+ public ICollection < T > Values
135+ {
136+ get { return client . GetHashValues ( this ) ; }
137+ }
138+
139+ public List < TKey > GetAllKeys ( )
140+ {
141+ return client . GetHashKeys ( this ) ;
142+ }
143+
144+ public List < T > GetAllValues ( )
145+ {
146+ return client . GetHashValues ( this ) ;
147+ }
148+ }
149149}
0 commit comments