-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathPostGreSQLDependency.cs
More file actions
73 lines (59 loc) · 2.05 KB
/
PostGreSQLDependency.cs
File metadata and controls
73 lines (59 loc) · 2.05 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
using Alachisoft.NCache.Runtime.Dependencies;
using Newtonsoft.Json;
using Npgsql;
using System;
using System.Data;
using System.Threading.Tasks;
namespace Alachisoft.NCache.Samples.PostGreSQLNotificationDependency
{
[Serializable]
public class PostGreSQLDependency : NotifyExtensibleDependency
{
private readonly string _connectionString;
private readonly string _dependencyKey;
private readonly string _schema;
private readonly string _table;
private readonly string _channel;
private IDbConnection connection;
private object task;
private bool done = false;
public PostGreSQLDependency(string connectionString, string dependencyKey, string schema, string table, string channel)
{
_connectionString = connectionString.Trim();
_dependencyKey = dependencyKey.Trim();
_schema = schema.Trim();
_table = table.Trim();
_channel = channel.Trim();
}
public override bool Initialize()
{
connection = new NpgsqlConnection(_connectionString);
connection.Open();
((NpgsqlConnection)connection).Notification += (o, e) =>
{
var entity = JsonConvert.DeserializeObject<Entity>(e.AdditionalInformation);
if (entity.DependencyKey == _dependencyKey && entity.Table == _table && entity.Schema == _schema)
{
done = true;
}
};
using (var cmd = new NpgsqlCommand($"LISTEN {_channel};", (NpgsqlConnection)connection))
{
cmd.ExecuteNonQuery();
}
task = Task.Run(() =>
{
while (true)
{
((NpgsqlConnection)connection).Wait();
if (done)
{
break;
}
}
this.DependencyChanged.Invoke(this);
});
return true;
}
}
}