Code Monkey home page Code Monkey logo

distributed's Introduction

Make your systems distributed, replicated, scaled well, easily.

Hex Version Docs Hex downloads GitHub MIT License

Tutorial

This is an example of a replicated GenServer.

defmodule Storage.KV do

	use GenServer

	def start_link() do
		GenServer.start_link(__MODULE__, [initial_state: %{}], name: __MODULE__.process_id())
	end

	def init(opts \\ []) do
		{:ok, Keyword.get(opts, :initial_state, %{})}
	end

	def process_id() do
		Storage.KV
	end

	def handle_cast({:set, key, value}, state) do
		{:noreply, Map.put(state, key, value)}
	end

	def handle_call({:get, key, default}, _from, state) do
		{:reply, Map.get(state, key, default), state}
	end

	def handle_call({:has, key}, _from, state) do
		{:reply, Map.has_key?(state, key), state}
	end

	def handle_call({:pop, key, default}, _from, state) do
		{value, new_state} = Map.pop(state, key, default)
		{:reply, value, new_state}
	end

	def get(key, default \\ nil) do
		Distributed.Scaler.GenServer.call(__MODULE__.process_id(), {:get, key, default})
	end

	def set(key, value) do
		{_node_name, result} = Distributed.Replicator.GenServer.cast(__MODULE__.process_id(), {:set, key, value})
			|> List.first()
		result
	end

	def has?(key) do
		Distributed.Scaler.GenServer.call(__MODULE__.process_id(), {:has, key})
	end

	def pop(key, default \\ nil) do
		{_node_name, result} = Distributed.Replicator.GenServer.call(__MODULE__.process_id(), {:pop, key, default})
			|> List.first()
		result
	end

end

You can see the example as a small project on ertgl/storage repository.

Installation:

If you have Hex, the package can be installed by adding :distributed to your list of dependencies in mix.exs:

def application do
	[
		extra_applications: [
			:distributed,
		],
	]
end

def deps do
	[
		{:distributed, "~> 0.1.3"},
	]
end

distributed's People

Contributors

ertgl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.