Skip to content
chapter_06
Go to file
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.org

simple_cache

The simple_cache application from Chapter 6 of Erlang and OTP in Action, ported to LFE.

Introduction

This is a fairly naïve implementation of an OTP application that provides a key/value store. It’s the second complete OTP app detailed in the book.

For an improved version with a gen_event-based logging system see the chapter_07 branch.

Installation

Just add it to your rebar.config deps:

{deps, [
  ...
  {simple_cache, ".*",
    {git, "git@github.com:yurrriq/simple_cache.git", "chapter_06"}}
    ]}.

And then do the usual:

$ rebar get-deps compile

Usage

Starting the application

Start the app from your terminal:

$ lfe -pa ebin -s simple-cache

… or call start/0

> (simple-cache:start)
ok

… or call application:start/1 to start the simple_cache application.

> (application:start 'simple_cache)
ok

Example variables

For convenience and brevity, we set an example key and an arbitrarily complex value in the REPL.

> (set key 'example)
example
> (set val '#(example #(string "This is an example") #(number 333)))
#(example #(string "This is an example") #(number 333))

Storing a key/value pair

To store a key/value pair in the cache, simply call simple-cache:insert/2. When successful, it returns the atom, true.

> (simple-cache:insert key val)
true

Otherwise, you’ll get an error like the following, most notably when the application isn’t started.

exception error: badarg
  in (: ets lookup sc-store example)
  in (sc-store lookup 1)
  in (simple-cache insert 2)

Looking up a value by key

To look up the value associated with a key, call lookup/1:

> (simple-cache:lookup key)
#(ok #(example #(string "This is an example") #(number 333)))

When the key is not found, you’ll get the tuple, #(error not-found).

> (simple-cache:lookup 'bad-key)
#(error not-found)

Note

simple-cache:lookup/1 will also return #(error not-found) when the application isn’t running.

> (application:stop 'simple_cache)

=INFO REPORT==== 18-Aug-2015::00:09:28 ===
    application: simple_cache
    exited: stopped
    type: temporary
ok
> (simple-cache:lookup key)
#(error not-found)

Deleting a key/value pair

delete/1 returns the atom, ok, even if the given key isn’t stored.

> (simple-cache:delete key)
ok
> (simple-cache:delete 'any-key)
ok

If you call lookup/1 with a deleted key, you’ll get the tuple, #(error not-found).

> (simple-cache:lookup key)
#(error not-found)

Default timeout

In this version, only the default timeout is used, though a quick code change would allow it to be specified manually. The default timeout is (* 60 60 24) or 86400 seconds or one day. After that long, the key/value pair will be deleted from the cache.

> (simple-cache:insert 'timeout "one day")
true
> (simple-cache:lookup 'timeout)
#(ok "one day")

Wait a day…

> (simple-cache:lookup 'timeout)
#(error not-found)

About

The simple_cache application from Erlang and OTP in Action, ported to LFE.

Resources

Releases

No releases published

Packages

No packages published
You can’t perform that action at this time.