@@ -52,6 +52,14 @@ public T Get<T>(string key)
52
52
) ;
53
53
}
54
54
55
+ private static byte [ ] ToBytes < T > ( T value )
56
+ {
57
+ var bytesValue = value as byte [ ] ;
58
+ if ( bytesValue == null && ! Equals ( value , default ( T ) ) )
59
+ bytesValue = value . ToJson ( ) . ToUtf8Bytes ( ) ;
60
+ return bytesValue ;
61
+ }
62
+
55
63
public long Increment ( string key , uint amount )
56
64
{
57
65
return Exec ( r => r . IncrementValueBy ( key , ( int ) amount ) ) ;
@@ -64,47 +72,24 @@ public long Decrement(string key, uint amount)
64
72
65
73
public bool Add < T > ( string key , T value )
66
74
{
67
- var bytesValue = value as byte [ ] ;
68
- if ( bytesValue != null )
69
- {
70
- return Exec ( r => r . SetNX ( key , bytesValue ) == Success ) ;
71
- }
72
-
73
- var valueString = JsonSerializer . SerializeToString ( value ) ;
74
- return Exec ( r => r . SetEntryIfNotExists ( key , valueString ) ) ;
75
+ return Exec ( r => r . Set ( key , ToBytes ( value ) , exists : false ) ) ;
75
76
}
76
77
77
78
public bool Set < T > ( string key , T value )
78
79
{
79
- var bytesValue = value as byte [ ] ;
80
- if ( bytesValue != null )
81
- {
82
- Exec ( r => ( ( RedisNativeClient ) r ) . Set ( key , bytesValue ) ) ;
83
- return true ;
84
- }
85
-
86
- Exec ( r => r . SetEntry ( key , JsonSerializer . SerializeToString ( value ) ) ) ;
80
+ Exec ( r => ( ( RedisNativeClient ) r ) . Set ( key , ToBytes ( value ) ) ) ;
87
81
return true ;
88
82
}
89
83
90
84
public bool Replace < T > ( string key , T value )
91
85
{
92
- var exists = ContainsKey ( key ) ;
93
- if ( ! exists ) return false ;
94
-
95
- var bytesValue = value as byte [ ] ;
96
- if ( bytesValue != null )
97
- {
98
- Exec ( r => ( ( RedisNativeClient ) r ) . Set ( key , bytesValue ) ) ;
99
- return true ;
100
- }
101
-
102
- Exec ( r => r . SetEntry ( key , JsonSerializer . SerializeToString ( value ) ) ) ;
103
- return true ;
86
+ return Exec ( r => r . Set ( key , ToBytes ( value ) , exists : true ) ) ;
104
87
}
105
88
106
89
public bool Add < T > ( string key , T value , DateTime expiresAt )
107
90
{
91
+ AssertNotInTransaction ( ) ;
92
+
108
93
return Exec ( r =>
109
94
{
110
95
if ( r . Add ( key , value ) )
@@ -118,34 +103,26 @@ public bool Add<T>(string key, T value, DateTime expiresAt)
118
103
119
104
public bool Add < T > ( string key , T value , TimeSpan expiresIn )
120
105
{
121
- return Exec ( r => {
122
- r . SetEntryIfNotExists ( key , value . ToJson ( ) , expiresIn ) ;
123
- return true ;
124
- } ) ;
106
+ return Exec ( r => r . Set ( key , ToBytes ( value ) , exists : false , expiryMs : ( long ) expiresIn . TotalMilliseconds ) ) ;
125
107
}
126
108
127
109
public bool Set < T > ( string key , T value , TimeSpan expiresIn )
128
110
{
129
- var bytesValue = value as byte [ ] ;
130
- if ( bytesValue != null )
111
+ if ( AssertServerVersionNumber ( ) >= 2600 )
131
112
{
132
- if ( AssertServerVersionNumber ( ) >= 2600 )
133
- {
134
- Exec ( r => r . PSetEx ( key , ( long ) expiresIn . TotalMilliseconds , bytesValue ) ) ;
135
- }
136
- else
137
- {
138
- Exec ( r => r . SetEx ( key , ( int ) expiresIn . TotalSeconds , bytesValue ) ) ;
139
- }
140
- return true ;
113
+ Exec ( r => r . Set ( key , ToBytes ( value ) , 0 , expiryMs : ( long ) expiresIn . TotalMilliseconds ) ) ;
114
+ }
115
+ else
116
+ {
117
+ Exec ( r => r . Set ( key , ToBytes ( value ) , ( int ) expiresIn . TotalSeconds ) ) ;
141
118
}
142
-
143
- Exec ( r => r . SetEntry ( key , JsonSerializer . SerializeToString ( value ) , expiresIn ) ) ;
144
119
return true ;
145
120
}
146
121
147
122
public bool Set < T > ( string key , T value , DateTime expiresAt )
148
123
{
124
+ AssertNotInTransaction ( ) ;
125
+
149
126
Exec ( r =>
150
127
{
151
128
Set ( key , value ) ;
@@ -156,6 +133,8 @@ public bool Set<T>(string key, T value, DateTime expiresAt)
156
133
157
134
public bool Replace < T > ( string key , T value , DateTime expiresAt )
158
135
{
136
+ AssertNotInTransaction ( ) ;
137
+
159
138
return Exec ( r =>
160
139
{
161
140
if ( r . Replace ( key , value ) )
@@ -169,15 +148,7 @@ public bool Replace<T>(string key, T value, DateTime expiresAt)
169
148
170
149
public bool Replace < T > ( string key , T value , TimeSpan expiresIn )
171
150
{
172
- return Exec ( r =>
173
- {
174
- if ( r . Replace ( key , value ) )
175
- {
176
- r . ExpireEntryIn ( key , expiresIn ) ;
177
- return true ;
178
- }
179
- return false ;
180
- } ) ;
151
+ return Exec ( r => r . Set ( key , ToBytes ( value ) , exists : true , expiryMs : ( long ) expiresIn . TotalMilliseconds ) ) ;
181
152
}
182
153
183
154
public IDictionary < string , T > GetAll < T > ( IEnumerable < string > keys )
0 commit comments