Skip to content

Commit

Permalink
Flexible caching
Browse files Browse the repository at this point in the history
  • Loading branch information
bausshf committed Sep 22, 2018
1 parent 74c012b commit a09393c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 10 deletions.
119 changes: 119 additions & 0 deletions app/appcache.d
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;
}
3 changes: 1 addition & 2 deletions app/server.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ static if (isWebServer)
import diamond.views.viewcache;

string pageResult;
auto routeName = client.route.name;

if (page.staticCache)
{
pageResult = getCachedView(routeName);
pageResult = getCachedView(client);
}
else if (webConfig.shouldCacheViews && page.cached)
{
Expand Down
3 changes: 3 additions & 0 deletions app/web.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static if (isWeb)
import diamond.authentication;
import diamond.security;
import diamond.unittesting;
import diamond.app.appcache;

static if (isWebServer)
{
Expand All @@ -39,6 +40,8 @@ static if (isWeb)
/// Initializes the Diamond run-time. This function does not initiate the server, tests, tasks, services etc.
void initializeDiamond()
{
setAppCache(new DiamondAppCache);

loadWebConfig();

if (webConfig.webservices && webConfig.webservices.length)
Expand Down
22 changes: 14 additions & 8 deletions views/viewcache.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
*/
module diamond.views.viewcache;

private __gshared string[string] _cache;
import diamond.http.client : HttpClient;

/**
* Caches a view.
* Params:
* route = The route to cache.
* client = The client to cache from.
* result = The result to cache.
* cacheTime = The time to cache the view. 0 equals process-lifetime.
*/
package(diamond) void cacheView(string route, string result, size_t cacheTime)
package(diamond) void cacheView(HttpClient client, string result, size_t cacheTime)
{
_cache[route] = result;
import diamond.app.appcache;

cache.updateCache(client, result);

if (cacheTime)
{
Expand All @@ -26,17 +28,21 @@ package(diamond) void cacheView(string route, string result, size_t cacheTime)
{
sleep(cacheTime.msecs);

_cache.remove(route);
cache.removeCache(client);
});
}
}

/**
* Gets the result of a cached view.
* Params:
* route = The route of the view to retrieve.
* client = The client to get the cached view from.
* Returns:
* The cached view result if present.
*/
string getCachedView(string route)
string getCachedView(HttpClient client)
{
return _cache.get(route, null);
import diamond.app.appcache;

return cache.retrieveCache(client);
}

0 comments on commit a09393c

Please sign in to comment.