This repository has been archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Cache store #132
Closed
Closed
Cache store #132
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ccbdaa4
Add ConCache as a dependency
anthonylebrun 90f3c9d
Setup config values for cache
anthonylebrun 550693c
Create cache store
anthonylebrun 8606af8
Optionally use cache store instead of regular store
anthonylebrun d87edab
Update README with cache usage
anthonylebrun 8b54271
Update examples to use cache
anthonylebrun bc32d62
Update method/convention for configuring the cache
anthonylebrun a7fc6ee
Decouple Thesis.CacheStore implementation from Thesis.EctoStore
anthonylebrun f4201cf
Add ability to programmatically clear Thesis.CacheStore
anthonylebrun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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,73 @@ | ||
defmodule Thesis.CacheStore do | ||
@moduledoc """ | ||
Thesis.CacheStore is a proxy store that acts in conjunction with a proper | ||
store like Thesis.EctoStore to provide caching and faster page load times. | ||
It can be enabled by setting `Thesis.Config.enable_cache` is set to `true`. | ||
""" | ||
|
||
@behaviour Thesis.Store | ||
|
||
import Thesis.Config | ||
|
||
@default_config [cache_name: :thesis_cache] | ||
@config Keyword.merge(Application.get_env(:thesis, __MODULE__, []), @default_config) | ||
|
||
def page(slug) do | ||
ConCache.get_or_store(@config[:cache_name], slug, fn() -> | ||
store().page(slug) | ||
end) | ||
end | ||
|
||
def page_contents(slug) when is_binary(slug) do | ||
page_contents(page(slug)) | ||
end | ||
|
||
def page_contents(nil) do | ||
cache_get(:global, fn() -> | ||
store().page_contents(nil) | ||
end) | ||
end | ||
|
||
def page_contents(page = %{id: page_id}) do | ||
cache_get(page_id, fn() -> | ||
store().page_contents(page) | ||
end) | ||
end | ||
|
||
def update(page_params = %{"slug" => slug}, contents_params) do | ||
cache_delete(slug) | ||
store().page_contents(slug) |> Enum.each(fn(%{page_id: page_id}) -> | ||
cache_delete(page_id) | ||
end) | ||
store().update(page_params, contents_params) | ||
end | ||
|
||
def delete(params = %{"slug" => slug}) do | ||
ConCache.delete(@config[:cache_name], slug) | ||
store().delete(params) | ||
end | ||
|
||
def restore(id) do | ||
store().restore(id) | ||
end | ||
|
||
def backups(page_slug_or_id) do | ||
store().backups(page_slug_or_id) | ||
end | ||
|
||
def clear_cache do | ||
@config[:cache_name] | ||
|> ConCache.ets | ||
|> :ets.tab2list | ||
|> Keyword.keys | ||
|> Enum.each(&cache_delete/1) | ||
end | ||
|
||
defp cache_get(key, fun) do | ||
ConCache.get_or_store(@config[:cache_name], key, fun) | ||
end | ||
|
||
defp cache_delete(key) do | ||
ConCache.delete(@config[:cache_name], key) | ||
end | ||
end |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe flesh out the paragraph here, give a little more background regarding why this is desirable.