Skip to content

Commit d29286f

Browse files
committed
Change the method calling way
1 parent cf50d69 commit d29286f

File tree

7 files changed

+86
-50
lines changed

7 files changed

+86
-50
lines changed

src/NRedisStack.Core/ModulPrefixes.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using StackExchange.Redis;
2+
3+
namespace NRedisStack.Core.RedisStackCommands
4+
{
5+
public static class ModulPrefixes
6+
{
7+
static public BloomCommands BF(this IDatabase db)
8+
{
9+
return new BloomCommands(db);
10+
}
11+
12+
static public SearchCommands FT(this IDatabase db)
13+
{
14+
return new SearchCommands(db);
15+
}
16+
17+
static public JsonCommands JSON(this IDatabase db)
18+
{
19+
return new JsonCommands(db);
20+
}
21+
22+
static public TimeSeriesCommands TS(this IDatabase db)
23+
{
24+
return new TimeSeriesCommands(db);
25+
}
26+
}
27+
}

src/NRedisStack.Core/RedisStackCommands/ TimeSeries.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
using StackExchange.Redis;
12
namespace NRedisStack.Core.RedisStackCommands
23
{
3-
public static class TS
4+
public class TimeSeriesCommands
45
{
5-
6+
IDatabase _db;
7+
public TimeSeriesCommands(IDatabase db)
8+
{
9+
_db = db;
10+
}
611
//TODO: INFO, CREATE, ALTER
712
/*public static string CREATE => "TS.CREATE";
813
public static string ALTER => "TS.ALTER";
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
using StackExchange.Redis;
22
namespace NRedisStack.Core.RedisStackCommands
33
{
4-
public static class RedisBloomCommands
4+
5+
public class BloomCommands
56
{
6-
public static RedisResult BfAdd(this IDatabase db, RedisKey key, string item)
7+
IDatabase _db;
8+
public BloomCommands(IDatabase db)
79
{
8-
return db.Execute("BF.ADD", key, item);
10+
_db = db;
911
}
10-
public static RedisResult BfExists(this IDatabase db, RedisKey key, string item)
12+
13+
14+
public RedisResult Add(RedisKey key, string item)
1115
{
12-
return db.Execute("BF.EXISTS", key, item);
16+
return _db.Execute("BF.ADD", key, item);
17+
}
18+
public RedisResult Exists(RedisKey key, string item)
19+
{
20+
return _db.Execute("BF.EXISTS", key, item);
1321
}
1422
/*public static string ADD => "BF.ADD";
1523
public static string EXISTS => "BF.EXISTS";
@@ -21,4 +29,6 @@ public static RedisResult BfExists(this IDatabase db, RedisKey key, string item)
2129
public static string RESERVE => "BF.RESERVE";
2230
public static string SCANDUMP => "BF.SCANDUMP";*/
2331
}
24-
}
32+
33+
34+
}

src/NRedisStack.Core/RedisStackCommands/Json.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22
using System.Text.Json;
33
using System.Text.Json.Serialization;
44

5-
namespace NRedisStack.Core.Json;
5+
namespace NRedisStack.Core.RedisStackCommands;
66

7-
public static class JSON
7+
public class JsonCommands
88
{
9-
private static readonly JsonSerializerOptions Options = new()
9+
IDatabase _db;
10+
public JsonCommands(IDatabase db)
11+
{
12+
_db = db;
13+
}
14+
private readonly JsonSerializerOptions Options = new()
1015
{
1116
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
1217
};
13-
public static RedisResult JsonSet(this IDatabase db, RedisKey key, string path, object obj, When when = When.Always)
18+
public RedisResult Set(RedisKey key, string path, object obj, When when = When.Always)
1419
{
1520
string json = JsonSerializer.Serialize(obj);
16-
return JsonSet(db, key, path, json, when);
21+
return Set(key, path, json, when);
1722
}
1823

19-
public static RedisResult JsonSet(this IDatabase db, RedisKey key, string path, string json, When when = When.Always)
24+
public RedisResult Set(RedisKey key, string path, string json, When when = When.Always)
2025
{
2126
switch (when)
2227
{
2328
case When.Exists:
24-
return db.Execute("JSON.SET", key, path, json, "XX");
29+
return _db.Execute("JSON.SET", key, path, json, "XX");
2530
case When.NotExists:
26-
return db.Execute("JSON.SET", key, path, json, "NX");
31+
return _db.Execute("JSON.SET", key, path, json, "NX");
2732
default:
28-
return db.Execute("JSON.SET", key, path, json);
33+
return _db.Execute("JSON.SET", key, path, json);
2934
}
3035
}
3136

32-
public static RedisResult JsonGet(this IDatabase db, RedisKey key, string indent = "",
37+
public RedisResult Get(RedisKey key, string indent = "",
3338
string newLine = "", string space = "", string path = "")
3439
{
3540
List<object> subcommands = new List<object>();
@@ -56,6 +61,6 @@ public static RedisResult JsonGet(this IDatabase db, RedisKey key, string indent
5661
{
5762
subcommands.Add(path);
5863
}
59-
return db.Execute("JSON.GET", subcommands.ToArray());
64+
return _db.Execute("JSON.GET", subcommands.ToArray());
6065
}
6166
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using StackExchange.Redis;
2-
namespace NRedisStack.Core.Search
2+
namespace NRedisStack.Core.RedisStackCommands
33
{
4-
public static class FT
4+
public class SearchCommands
55
{
6-
public static RedisResult FtInfo(this IDatabase db, string index)
6+
IDatabase _db;
7+
public SearchCommands(IDatabase db)
78
{
8-
return db.Execute("FT.INFO", index);
9+
_db = db;
10+
}
11+
public RedisResult FtInfo(string index)
12+
{
13+
return _db.Execute("FT.INFO", index);
914
}
1015
}
1116
}
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
using Xunit;
2-
using System;
3-
using System.Collections.Generic;
42
using StackExchange.Redis;
5-
using System.Linq;
6-
using System.IO;
7-
using NRedisStack.Core;
83
using NRedisStack.Core.RedisStackCommands;
94
using Moq;
105

11-
using System.Text.Json;
12-
using System.Text.Json.Serialization;
13-
146

157
namespace NRedisStack.Tests.Bloom;
168

@@ -30,16 +22,16 @@ public void TestBfAddWhenExist()
3022
{
3123
IDatabase db = redisFixture.Redis.GetDatabase();
3224

33-
Assert.True(db.BfAdd(key, "item1").ToString() == "1"); // first time
34-
Assert.True(db.BfAdd(key, "item1").ToString() == "0"); // second time
25+
Assert.True((db.BF().Add(key, "item1")).ToString() == "1"); // first time
26+
Assert.True(db.BF().Add(key, "item1").ToString() == "0"); // second time
3527
}
3628

3729
[Fact]
3830
public void TestBfAddExists()
3931
{
4032
IDatabase db = redisFixture.Redis.GetDatabase();
4133

42-
db.BfAdd(key, "item1");
43-
Assert.True(db.BfExists(key, "item1").ToString() == "1");
34+
db.BF().Add(key, "item1");
35+
Assert.True(db.BF().Exists(key, "item1").ToString() == "1");
4436
}
4537
}
Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
using Xunit;
2-
using System;
3-
using System.Collections.Generic;
42
using StackExchange.Redis;
5-
using System.Linq;
6-
using System.IO;
7-
using NRedisStack.Core;
8-
using NRedisStack.Core.Json;
93
using Moq;
10-
11-
using System.Text.Json;
12-
using System.Text.Json.Serialization;
4+
using NRedisStack.Core.RedisStackCommands;
135

146

157
namespace NRedisStack.Tests;
@@ -29,15 +21,15 @@ public void Dispose()
2921
public void TestJsonSet()
3022
{
3123
var obj = new Person { Name = "Shachar", Age = 23 };
32-
_mock.Object.JsonSet("Person:Shachar", "$", obj, When.Exists);
24+
_mock.Object.JSON().Set("Person:Shachar", "$", obj, When.Exists);
3325
_mock.Verify(x => x.Execute("JSON.SET", "Person:Shachar", "$", "{\"Name\":\"Shachar\",\"Age\":23}", "XX"));
3426
}
3527

3628
[Fact]
3729
public void TestJsonSetNotExist()
3830
{
3931
var obj = new Person { Name = "Shachar", Age = 23 };
40-
_mock.Object.JsonSet("Person:Shachar", "$", obj, When.NotExists);
32+
_mock.Object.JSON().Set("Person:Shachar", "$", obj, When.NotExists);
4133
_mock.Verify(x => x.Execute("JSON.SET", "Person:Shachar", "$", "{\"Name\":\"Shachar\",\"Age\":23}", "NX"));
4234
}
4335

@@ -47,9 +39,9 @@ public void TestSimpleJsonGet()
4739
var obj = new Person { Name = "Shachar", Age = 23 };
4840
IDatabase db = redisFixture.Redis.GetDatabase();
4941

50-
db.JsonSet(key, "$", obj);
42+
db.JSON().Set(key, "$", obj);
5143
string expected = "{\"Name\":\"Shachar\",\"Age\":23}";
52-
Assert.Equal(db.JsonGet(key).ToString(), expected);
44+
Assert.Equal(db.JSON().Get(key).ToString(), expected);
5345
}
5446

5547
[Fact]
@@ -58,9 +50,9 @@ public void TestJsonGet()
5850
var obj = new Person { Name = "Shachar", Age = 23 };
5951
IDatabase db = redisFixture.Redis.GetDatabase();
6052

61-
db.JsonSet(key, "$", obj);
53+
db.JSON().Set(key, "$", obj);
6254

6355
string expected = "[222111\"Shachar\"222]";
64-
Assert.Equal(db.JsonGet(key, "111", "222", "333", "$.Name").ToString(), expected);
56+
Assert.Equal(db.JSON().Get(key, "111", "222", "333", "$.Name").ToString(), expected);
6557
}
6658
}

0 commit comments

Comments
 (0)