Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System.Collections.Generic;
10 : using Microsoft.Azure.Documents;
11 : using Microsoft.Azure.Documents.Client;
12 :
13 : namespace Cqrs.Azure.DocumentDb
14 : {
15 : /// <summary>
16 : /// A cache manager for DocumentDB clients, databases and collections that is global.
17 : /// </summary>
18 : public class GlobalAzureDocumentDbConnectionCache : IAzureDocumentDbConnectionCache
19 1 : {
20 : /// <summary>
21 : /// Gets the <see cref="IDictionary{Tstring,Tobject}">cache</see> used.
22 : /// </summary>
23 : protected static IDictionary<string, object> Cache { get; private set; }
24 :
25 : static GlobalAzureDocumentDbConnectionCache()
26 : {
27 : Cache = new Dictionary<string, object>();
28 : }
29 :
30 : /// <summary>
31 : /// Gets the <see cref="DocumentClient"/>.
32 : /// </summary>
33 : /// <param name="key">The name of the <see cref="DocumentClient"/> to get.</param>
34 : /// <param name="client">If the <see cref="DocumentClient"/> is found, it is returned here; otherwise null is returned. This parameter is passed uninitialized.</param>
35 : /// <returns>true if the <see cref="DocumentClient"/> is found; otherwise, false.</returns>
36 1 : public bool TryGetClient(string key, out DocumentClient client)
37 : {
38 : try
39 : {
40 : object cacheResult;
41 : bool result = Cache.TryGetValue(key, out cacheResult);
42 : client = cacheResult as DocumentClient;
43 : return result && client != null;
44 : }
45 : catch
46 : {
47 : }
48 : client = null;
49 : return false;
50 : }
51 :
52 : /// <summary>
53 : /// Sets the provided <paramref name="client"/>.
54 : /// </summary>
55 : /// <param name="key">The name of the <see cref="DocumentClient"/> to get.</param>
56 : /// <param name="client">The <see cref="DocumentClient"/> to set.</param>
57 1 : public void SetClient(string key, DocumentClient client)
58 : {
59 : Cache[key] = client;
60 : }
61 :
62 : /// <summary>
63 : /// Gets the <see cref="Database"/>.
64 : /// </summary>
65 : /// <param name="key">The name of the <see cref="Database"/> to get.</param>
66 : /// <param name="database">If the <see cref="Database"/> is found, it is returned here; otherwise null is returned. This parameter is passed uninitialized.</param>
67 : /// <returns>true if the <see cref="Database"/> is found; otherwise, false.</returns>
68 1 : public bool TryGetDatabase(string key, out Database database)
69 : {
70 : try
71 : {
72 : object cacheResult;
73 : bool result = Cache.TryGetValue(key, out cacheResult);
74 : database = cacheResult as Database;
75 : return result && database != null;
76 : }
77 : catch
78 : {
79 : }
80 : database = null;
81 : return false;
82 : }
83 :
84 : /// <summary>
85 : /// Sets the provided <paramref name="database"/>.
86 : /// </summary>
87 : /// <param name="key">The name of the <see cref="Database"/> to get.</param>
88 : /// <param name="database">The <see cref="Database"/> to set.</param>
89 1 : public void SetDatabase(string key, Database database)
90 : {
91 : Cache[key] = database;
92 : }
93 :
94 : /// <summary>
95 : /// Gets the <see cref="DocumentCollection"/>.
96 : /// </summary>
97 : /// <param name="key">The name of the <see cref="DocumentCollection"/> to get.</param>
98 : /// <param name="documentCollection">If the <see cref="DocumentCollection"/> is found, it is returned here; otherwise null is returned. This parameter is passed uninitialized.</param>
99 : /// <returns>true if the <see cref="DocumentCollection"/> is found; otherwise, false.</returns>
100 1 : public bool TryGetDocumentCollection(string key, out DocumentCollection documentCollection)
101 : {
102 : try
103 : {
104 : object cacheResult;
105 : bool result = Cache.TryGetValue(key, out cacheResult);
106 : documentCollection = cacheResult as DocumentCollection;
107 : return result && documentCollection != null;
108 : }
109 : catch
110 : {
111 : }
112 : documentCollection = null;
113 : return false;
114 : }
115 :
116 : /// <summary>
117 : /// Sets the provided <paramref name="documentCollection"/>.
118 : /// </summary>
119 : /// <param name="key">The name of the <see cref="DocumentCollection"/> to get.</param>
120 : /// <param name="documentCollection">The <see cref="DocumentCollection"/> to set.</param>
121 1 : public void SetDocumentCollection(string key, DocumentCollection documentCollection)
122 : {
123 : Cache[key] = documentCollection;
124 : }
125 : }
126 : }
|