-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* Copyright © DiamondMVC 2018 | ||
* License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE) | ||
* Author: Jacob Jensen (bausshf) | ||
*/ | ||
module diamond.app.appcache; | ||
|
||
/// Interface for an app cache. | ||
interface IAppCache | ||
{ | ||
/** | ||
* Updates the app cache. | ||
* Params: | ||
* client = The client to cache from. | ||
* defaultResult = The default result to cache. | ||
*/ | ||
void updateCache(HttpClient client, string defaultResult); | ||
|
||
/** | ||
* Retrieves the cached result based on a client. | ||
* Params: | ||
* client = The client to receive the cached result based on. | ||
* Returns: | ||
* The cached result. | ||
*/ | ||
string retrieveCache(HttpClient client); | ||
|
||
/// Clears the cache results. | ||
void clearCache(); | ||
|
||
/** | ||
* Removes a cached result. | ||
* Params: | ||
* client = The client to remove the cached result based on. | ||
*/ | ||
void removeCache(HttpClient client); | ||
} | ||
|
||
/** | ||
* Sets the app cache. | ||
* Params: | ||
* cache = The app cache. | ||
*/ | ||
void setAppCache(IAppCache cache) | ||
{ | ||
if (!cache) | ||
{ | ||
return; | ||
} | ||
|
||
_cache = cache; | ||
} | ||
|
||
private | ||
{ | ||
/// Wrapper around the default diamond app cache. | ||
class DiamondAppCache : IAppCache | ||
{ | ||
private: | ||
/// The cache. | ||
string[string] _cache; | ||
|
||
public: | ||
/// Creates a new diamond app cache. | ||
this() | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* Updates the app cache. | ||
* Params: | ||
* client = The client to cache from. | ||
* defaultResult = The default result to cache. | ||
*/ | ||
void updateCache(HttpClient client, string defaultResult) | ||
{ | ||
_cache[client.route.name] = defaultResult; | ||
} | ||
|
||
/** | ||
* Retrieves the cached result based on a client. | ||
* Params: | ||
* client = The client to receive the cached result based on. | ||
* Returns: | ||
* The cached result. | ||
*/ | ||
string retrieveCache(HttpClient client) | ||
{ | ||
return _cache.get(client.route.name, null); | ||
} | ||
|
||
/// Clears the cache results. | ||
void clearCache() | ||
{ | ||
_cache.clear(); | ||
} | ||
|
||
/** | ||
* Removes a cached result. | ||
* Params: | ||
* client = The client to remove the cached result based on. | ||
*/ | ||
void removeCache(HttpClient client) | ||
{ | ||
_cache.remove(client.route.name); | ||
} | ||
} | ||
|
||
/// The cache. | ||
__gshared IAppCache _cache; | ||
} | ||
|
||
package(diamond): | ||
/// Gets the app cache. | ||
@property IAppCache cache() | ||
{ | ||
return _cache; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters