LEFT | RIGHT |
1 /* | 1 /* |
2 Copyright 2011 Google Inc | 2 Copyright 2011 Google Inc |
3 | 3 |
4 Licensed under the Apache License, Version 2.0 (the "License"); | 4 Licensed under the Apache License, Version 2.0 (the "License"); |
5 you may not use this file except in compliance with the License. | 5 you may not use this file except in compliance with the License. |
6 You may obtain a copy of the License at | 6 You may obtain a copy of the License at |
7 | 7 |
8 http://www.apache.org/licenses/LICENSE-2.0 | 8 http://www.apache.org/licenses/LICENSE-2.0 |
9 | 9 |
10 Unless required by applicable law or agreed to in writing, software | 10 Unless required by applicable law or agreed to in writing, software |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 if (!Directory.Exists(folderPath)) | 44 if (!Directory.Exists(folderPath)) |
45 { | 45 { |
46 Directory.CreateDirectory(folderPath); | 46 Directory.CreateDirectory(folderPath); |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 /// <summary> | 50 /// <summary> |
51 /// Stores the given value for the given key. It creates a new file (nam
ed <see cref="GetStoredKey"/>) in· | 51 /// Stores the given value for the given key. It creates a new file (nam
ed <see cref="GetStoredKey"/>) in· |
52 /// <see cref="FolderPath"/>. | 52 /// <see cref="FolderPath"/>. |
53 /// </summary> | 53 /// </summary> |
54 /// <typeparam name="T">Type to store in the data store</typeparam> | 54 /// <typeparam name="T">The type to store in the data store</typeparam> |
55 /// <param name="key">The key</param> | 55 /// <param name="key">The key</param> |
56 /// <param name="value">The value to store in the data store</param> | 56 /// <param name="value">The value to store in the data store</param> |
57 public Task Store<T>(string key, T value) | 57 public Task Store<T>(string key, T value) |
58 { | 58 { |
59 if (string.IsNullOrEmpty(key)) | 59 if (string.IsNullOrEmpty(key)) |
60 { | 60 { |
61 throw new ArgumentException("Key MUST have a value"); | 61 throw new ArgumentException("Key MUST have a value"); |
62 } | 62 } |
63 | 63 |
64 var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value); | 64 var serialized = NewtonsoftJsonSerializer.Instance.Serialize(value); |
65 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T))
); | 65 var filePath = Path.Combine(folderPath, GetStoredKey(key, typeof(T))
); |
66 File.WriteAllText(filePath, serialized); | 66 File.WriteAllText(filePath, serialized); |
67 return TaskEx.Delay(0); | 67 return TaskEx.Delay(0); |
68 } | 68 } |
69 | 69 |
70 /// <summary> | 70 /// <summary> |
71 /// Deletes the given key. It deletes the <see cref="GetStoredKey"/> nam
ed file in <see cref="FolderPath"/>. | 71 /// Deletes the given key. It deletes the <see cref="GetStoredKey"/> nam
ed file in <see cref="FolderPath"/>. |
72 /// </summary> | 72 /// </summary> |
73 /// <param name="key">The key to delete from the data store</param> | 73 /// <param name="key">The key to delete from the data store</param> |
74 /// <param name="t">The type of the stored value.</param> | 74 /// <param name="t">The type of the stored value</param> |
75 public Task Delete(string key, Type t) | 75 public Task Delete(string key, Type t) |
76 { | 76 { |
77 if (string.IsNullOrEmpty(key)) | 77 if (string.IsNullOrEmpty(key)) |
78 { | 78 { |
79 throw new ArgumentException("Key MUST have a value"); | 79 throw new ArgumentException("Key MUST have a value"); |
80 } | 80 } |
81 if (t == null) | 81 if (t == null) |
82 { | 82 { |
83 throw new ArgumentException("Type can't be null"); | 83 throw new ArgumentException("Type can't be null"); |
84 } | 84 } |
85 | 85 |
86 var filePath = Path.Combine(folderPath, GetStoredKey(key, t)); | 86 var filePath = Path.Combine(folderPath, GetStoredKey(key, t)); |
87 if (File.Exists(filePath)) | 87 if (File.Exists(filePath)) |
88 { | 88 { |
89 File.Delete(filePath); | 89 File.Delete(filePath); |
90 } | 90 } |
91 return TaskEx.Delay(0); | 91 return TaskEx.Delay(0); |
92 } | 92 } |
93 | 93 |
94 /// <summary> | 94 /// <summary> |
95 /// Returns the stored value for the given key or <c>null</c> if the ma
tching file (<see cref="GetStoredKey"/> | 95 /// Returns the stored value for the given key or <c>null</c> if the mat
ching file (<see cref="GetStoredKey"/> |
96 /// in <see cref="FolderPath"/> doesn't exists. | 96 /// in <see cref="FolderPath"/> doesn't exist. |
97 /// </summary> | 97 /// </summary> |
98 /// <typeparam name="T">The type to retrieve</typeparam> | 98 /// <typeparam name="T">The type to retrieve</typeparam> |
99 /// <param name="key">The key to retrieve from the data store</param> | 99 /// <param name="key">The key to retrieve from the data store</param> |
100 /// <returns>The stored object</returns> | 100 /// <returns>The stored object</returns> |
101 public Task<T> Get<T>(string key) | 101 public Task<T> Get<T>(string key) |
102 { | 102 { |
103 if (string.IsNullOrEmpty(key)) | 103 if (string.IsNullOrEmpty(key)) |
104 { | 104 { |
105 throw new ArgumentException("Key MUST have a value"); | 105 throw new ArgumentException("Key MUST have a value"); |
106 } | 106 } |
(...skipping 13 matching lines...) Expand all Loading... |
120 } | 120 } |
121 } | 121 } |
122 else | 122 else |
123 { | 123 { |
124 tcs.SetResult(default(T)); | 124 tcs.SetResult(default(T)); |
125 } | 125 } |
126 return tcs.Task; | 126 return tcs.Task; |
127 } | 127 } |
128 | 128 |
129 /// <summary> | 129 /// <summary> |
130 /// Clear all values in the data store. This method deletes all files in
<see cref="FolderPath"/>. | 130 /// Clears all values in the data store. This method deletes all files i
n <see cref="FolderPath"/>. |
131 /// </summary> | 131 /// </summary> |
132 public Task Clear() | 132 public Task Clear() |
133 { | 133 { |
134 if (Directory.Exists(folderPath)) | 134 if (Directory.Exists(folderPath)) |
135 { | 135 { |
136 Directory.Delete(folderPath, true); | 136 Directory.Delete(folderPath, true); |
137 Directory.CreateDirectory(folderPath); | 137 Directory.CreateDirectory(folderPath); |
138 } | 138 } |
139 | 139 |
140 return TaskEx.Delay(0); | 140 return TaskEx.Delay(0); |
141 } | 141 } |
142 | 142 |
143 /// <summary>Creates a unique stored key based on the key and the class
type.</summary> | 143 /// <summary>Creates a unique stored key based on the key and the class
type.</summary> |
144 /// <param name="key">The object key</param> | 144 /// <param name="key">The object key</param> |
145 /// <param name="t">The type to store or retrieve</param> | 145 /// <param name="t">The type to store or retrieve</param> |
146 public static string GetStoredKey(string key, Type t) | 146 public static string GetStoredKey(string key, Type t) |
147 { | 147 { |
148 return string.Format("{0}-{1}", t.FullName, key); | 148 return string.Format("{0}-{1}", t.FullName, key); |
149 } | 149 } |
150 } | 150 } |
151 } | 151 } |
LEFT | RIGHT |