{"id":30376594,"url":"https://github.com/ckampfe/cas","last_synced_at":"2025-08-20T15:01:53.651Z","repository":{"id":300134996,"uuid":"1005302327","full_name":"ckampfe/cas","owner":"ckampfe","description":"Provides Cas.Cell, a direct analog to Clojure's atom, to provide (as Clojure says) \"a way to manage shared, synchronous, independent state\".","archived":false,"fork":false,"pushed_at":"2025-07-24T02:40:12.000Z","size":17,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-24T05:52:48.516Z","etag":null,"topics":["clojure","concurrency","elixir","elixir-lang","software-transactional-memory"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ckampfe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-06-20T02:41:56.000Z","updated_at":"2025-07-24T02:40:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"5962a028-5205-4dc0-b994-e89a58ca6abd","html_url":"https://github.com/ckampfe/cas","commit_stats":null,"previous_names":["ckampfe/cas"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ckampfe/cas","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckampfe%2Fcas","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckampfe%2Fcas/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckampfe%2Fcas/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckampfe%2Fcas/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ckampfe","download_url":"https://codeload.github.com/ckampfe/cas/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ckampfe%2Fcas/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271337859,"owners_count":24742057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clojure","concurrency","elixir","elixir-lang","software-transactional-memory"],"created_at":"2025-08-20T15:01:14.651Z","updated_at":"2025-08-20T15:01:53.646Z","avatar_url":"https://github.com/ckampfe.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cas\n\n\n\nProvides `Cas.Cell`, a direct analog to Clojure's [atom](https://clojure.org/reference/atoms), to provide (as Clojure says) \"a way to manage shared, synchronous, independent state\".\n\nAn cell is as an alternative to Elixir's [Agent](https://hexdocs.pm/elixir/1.18.4/Agent.html), or building your own GenServer to manage a piece of state. It's specifically useful when you have a piece of state that you want to share between and update from a number of processes.\n\n\n[![Elixir CI](https://github.com/ckampfe/cas/actions/workflows/elixir.yml/badge.svg)](https://github.com/ckampfe/cas/actions/workflows/elixir.yml)\n\n## Examples\n\n```elixir\nalias Cas.Cell\n\n# Create an cell\ncell = Cell.new(1)\n\n# Get the value of an cell\nCell.get(cell)\n#=\u003e 1\n\n# Swap values into an cell by applying a function to the\n# current value of the cell.\n# This is free from race conditions between the read of the current\n# value and writing to the current value, because the\n# underlying ETS API guarantees that `select_replace/2` is\n# atomic and isolated.\n#\n# This means that an arbitrary number of processes can be swapping\n# things into the cell concurrently, and they will always update the cell atomically.\n# More concretely, this means the value passed to `f` will never be outdated.\n# The value returned by `f` will always be derived from the most recent value of the cell,\n# uncorrupted by other concurrent writers.\nCell.swap!(cell, fn i -\u003e i + 1 end)\n#=\u003e 2\nCell.swap!(cell, fn i -\u003e i + 1 end)\n#=\u003e 3\n\n# reset the value of the cell\nCell.reset!(cell, 99)\n#=\u003e 99\n\n# same as `swap!`, but return both the old value and the new value\nCell.swap_old_and_new!(cell, fn i -\u003e i + 1 end)\n#=\u003e {99, 100}\n\n# delete an cell's backing storage\nCell.delete(cell)\n#=\u003e true\n```\n\n## More detail\n\n`Cas.Cell` uses ETS rather than processes, so it avoids the overhead of process mailboxes and message sends in favor of atomic compare-and-swaps in ETS itself.\n\nThis is possible because ETS provides a mechanism to do atomic compare-and-swap via [select_replace/2](https://www.erlang.org/doc/apps/stdlib/ets.html#select_replace/2). I only recently discovered that ETS provided this functionality, and once I did I knew I had to build this.\n\nBecause `Cas.Cell` uses ETS, unused cells will leak if they are not deleted, since they correspond 1:1 with rows in an ETS table.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckampfe%2Fcas","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fckampfe%2Fcas","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fckampfe%2Fcas/lists"}