hardcache

Types

pub type Cache {
  Cache(
    file_path: String,
    entries: List(#(String, String)),
    auto_update: Bool,
  )
}

Constructors

  • Cache(
      file_path: String,
      entries: List(#(String, String)),
      auto_update: Bool,
    )
pub type CacheError {
  CacheError(String)
  FileError(file_error.FileError)
  ReadStreamError(read_stream_error.ReadStreamError)
}

Constructors

  • CacheError(String)
  • FileError(file_error.FileError)
  • ReadStreamError(read_stream_error.ReadStreamError)

Functions

pub fn auto_update(cache: Cache) -> Cache

Sets the auto_update field of the cache to True.

Examples

let cache = hardcache.new("./example.txt", False)
let cache = case cache {
  Ok(cache) -> Ok(hardcache.auto_update(cache))
  Error(e) -> Error(e)
}
pub fn defaults(
  in orig: Cache,
  defaults entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets a key-value pair in the list if it is not already set.

Examples

let cache = case hardcache.new("./example.txt", True) {
  Ok(cache) -> hardcache.defaults(cache, [#("msg", "Hello!")])
  err -> err
}
pub fn get(from cache: Cache, key key: String) -> Option(String)

Gets the value associated with a key in the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
let a = case cache {
  Ok(cache) ->
    cache
    |> hardcache.get("a")
  Error(_) -> option.None
}
pub fn new(
  path file_path: String,
  auto_update auto_update: Bool,
) -> Result(Cache, CacheError)

Creates a new cache by reading from the specified file path.

Examples

let cache = hardcache.new("./example.txt", True)
pub fn parse_entries(string: String) -> List(#(String, String))

Parses a string into key-value pairs.

Example

hardcache.parse_entries("!key=value\n")
// -> [#("key", "value")]
pub fn remove(
  from orig: Cache,
  key key: String,
) -> Result(Cache, CacheError)

Removes a key-value pair from the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
// -> Ok(Cache("./example.txt", "!a=A\n", True))
let cache = case cache {
  Ok(cache) ->
    cache
    |> hardcache.remove("a")
  Error(e) -> Error(e)
}
// -> Ok(Cache("./example.txt", "", True))
pub fn set(
  in orig: Cache,
  key key: String,
  value value: String,
) -> Result(Cache, CacheError)

Sets a key-value pair in the cache.

Examples

let cache = hardcache.new("./example.txt", True)
let cache = case cache {
  Ok(cache) ->
    cache
    |> hardcache.set("a", "A")
  Error(e) -> Error(e)
}
pub fn set_many(
  in orig: Cache,
  entries entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets multiple key-value pairs in the cache.

Examples

let cache = hardcache.new("./example.txt", True)
let cache = case cache {
  Ok(cache) ->
    cache
    |> hardcache.set_many([#("a", "A"), #("b", "B")])
  Error(e) -> Error(e)
}
pub fn stringify_entries(
  entries: List(#(String, String)),
) -> String

Converts key-value pairs to a string.

Example

hardcache.stringify_entries([#("key", "value")])
// -> "!key=value\n"
pub fn try_defaults(
  in orig: Result(Cache, CacheError),
  defaults entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets a key-value pair in the list if it is not already set.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_defaults(cache, [#("msg", "Hello!")])
pub fn try_get(
  from orig: Result(Cache, CacheError),
  key key: String,
) -> Result(Option(String), CacheError)

Sets multiple key-value pairs in the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
let a =
  cache
  |> hardcache.try_get("a")
pub fn try_remove(
  from orig: Result(Cache, CacheError),
  key key: String,
) -> Result(Cache, CacheError)

Sets a key-value pair in the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
// -> Ok(Cache("./example.txt", "!a=A\n", True))
let cache =
  cache
  |> hardcache.try_remove("a")
// -> Ok(Cache("./example.txt", "", True))
pub fn try_set(
  in orig: Result(Cache, CacheError),
  key key: String,
  value value: String,
) -> Result(Cache, CacheError)

Sets a key-value pair in the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set("a", "A")
pub fn try_set_many(
  in orig: Result(Cache, CacheError),
  entries entries: List(#(String, String)),
) -> Result(Cache, CacheError)

Sets multiple key-value pairs in the cache.

Examples

let cache =
  hardcache.new("./example.txt", True)
  |> hardcache.try_set_many([#("a", "A"), #("b", "B")])
pub fn try_update(
  cache: Result(Cache, CacheError),
) -> Result(Cache, CacheError)

Updates the cache file with the current cache content.

Examples

let cache =
  hardcache.new("./example.txt", False)
  |> hardcache.try_set("a", "A")
  |> hardcache.try_set("b", "B")
  |> hardcache.try_set_many([#("c", "C")])
  |> hardcache.try_update
pub fn update(cache: Cache) -> Result(Cache, CacheError)

Updates the cache file with the current cache content.

Examples

let cache =
  hardcache.new("./example.txt", False)
  |> hardcache.try_set("a", "A")
  |> hardcache.try_set("b", "B")
  |> hardcache.try_set_many([#("c", "C")])

let cache = case cache {
  Ok(cache) -> hardcache.update(cache)
  Error(e) -> Error(e)
}
Search Document