Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/builtin_mcc/.formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions example/builtin_mcc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where 3rd-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
builtin_mcc-*.tar

1 change: 1 addition & 0 deletions example/builtin_mcc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# BuiltinMcc
4 changes: 4 additions & 0 deletions example/builtin_mcc/config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use Mix.Config

config :mcc,
mnesia_table_modules: [BuiltinMcc.Cache]
26 changes: 26 additions & 0 deletions example/builtin_mcc/lib/builtin_mcc.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule BuiltinMcc do
@moduledoc """
Documentation for BuiltinMcc.
"""

alias BuiltinMcc.Table.AWithExp

def test_table_a_with_exp do
IO.puts("put one key pair: `{k1, v1}`, and ttl is 1")
AWithExp.put_with_ttl(:k1, :v1, 1)
IO.puts("get the key pair for `k1`")
IO.puts("the cache data for key `k1` is: #{inspect(AWithExp.check_cache_data(:k1))}")
IO.puts("sleep 1 second...")
Process.sleep(1000)
IO.puts("the value for key `k1` is: #{inspect(AWithExp.get_with_ttl(:k1))}")
IO.puts("===========================================")
IO.puts("put one key pair: `{k2, v2}`, and ttl is 2")
AWithExp.put_with_ttl(:k2, :v2, 2)
IO.puts("get the key pair for `k2`")
IO.puts("the cache data for key `k2` is: #{inspect(AWithExp.check_cache_data(:k2))}")
IO.puts("sleep 1 second...")
Process.sleep(1000)
IO.puts("the value for key `k1` is: #{inspect(AWithExp.get_with_ttl(:k2, 2))}")
IO.puts("the cache data for key `k1` is: #{inspect(AWithExp.check_cache_data(:k2))}")
end
end
12 changes: 12 additions & 0 deletions example/builtin_mcc/lib/builtin_mcc/application.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule BuiltinMcc.Application do
@moduledoc false

use Application

def start(_type, _args) do
children = []
opts = [strategy: :one_for_one, name: BuiltinMcc.Supervisor]
BuiltinMcc.Cache.start_expiration_process()
Supervisor.start_link(children, opts)
end
end
10 changes: 10 additions & 0 deletions example/builtin_mcc/lib/builtin_mcc/cache.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
defmodule BuiltinMcc.Cache do
@moduledoc """
BuiltinMcc cache.
"""

use Mcc.Model

import_table(BuiltinMcc.Table.AWithExp.Exp)
import_table(BuiltinMcc.Table.AWithExp)
end
75 changes: 75 additions & 0 deletions example/builtin_mcc/lib/builtin_mcc/table/a_with_exp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
defmodule BuiltinMcc.Table.AWithExp.Exp do
@moduledoc """
Table for expiration.
"""

use Mcc.Model.Table,
table_opts: [
type: :ordered_set,
ram_copies: [node()],
storage_properties: [
ets: [:compressed, read_concurrency: true]
]
]

defstruct [:key, :value]
end

defmodule BuiltinMcc.Table.AWithExp do
@moduledoc """
One table with expiration.
The original data for this table only maint by current service.

And there are no other service could operate the original data
for this cache table.

So, current service can continue the ttl when read data from table.
"""

alias BuiltinMcc.Table.AWithExp.Exp, as: AWithExpExp

use Mcc.Model.Table,
table_opts: [
type: :set,
ram_copies: [node()],
storage_properties: [
ets: [:compressed, read_concurrency: true]
]
],
expiration_opts: [
expiration_table: AWithExpExp,
main_table: __MODULE__,
size_limit: 100,
# 300M
memory_limit: 300,
waterline_ratio: 0.7,
check_interval: 1_000
]

defstruct([:key, :value], true)

def check_cache_data(k) do
get(k)
end

def get_with_ttl(k, ttl \\ 10) do
case get(k) do
%{key: ^k, value: _v} = old_object ->
put(k, old_object, ttl)
old_object

_ ->
:"$not_can_found"
end
end

def put_with_ttl(k, v, ttl \\ 10) do
put(k, %__MODULE__{key: k, value: v}, ttl)
end

def sync_dirty_put(k, v, ttl \\ 10) do
put(k, %__MODULE__{key: k, value: v}, ttl, :sync_dirty)
end

# __end_of_module__
end
26 changes: 26 additions & 0 deletions example/builtin_mcc/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule BuiltinMcc.MixProject do
use Mix.Project

def project do
[
app: :builtin_mcc,
version: "0.1.0",
elixir: "~> 1.7",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

def application do
[
extra_applications: [:logger],
mod: {BuiltinMcc.Application, []}
]
end

defp deps do
[
{:mcc, path: "../../../mcc"}
]
end
end