An open API service indexing awesome lists of open source software.

https://github.com/manuelgeek/learn-genserver

OTP in Elixir: Learn GenServer by Building Your Own URL Shortener
https://github.com/manuelgeek/learn-genserver

elixir-lang genserver otp

Last synced: 2 months ago
JSON representation

OTP in Elixir: Learn GenServer by Building Your Own URL Shortener

Awesome Lists containing this project

README

        

# LearnGenserver

> Learn GenServer by Building Your Own URL Shortener

## Running

### Example

```elixir
iex(22)> shortener = URLShortener.start
#PID<0.141.0>

iex(23)> send shortener, {:shorten, "https://ieftimov.com", self()}
{:shorten, "https://ieftimov.com", #PID<0.102.0>}

iex(24)> send shortener, {:shorten, "https://google.com", self()}
{:shorten, "https://google.com", #PID<0.102.0>}

iex(25)> send shortener, {:shorten, "https://github.com", self()}
{:shorten, "https://github.com", #PID<0.102.0>}

iex(26)> flush
"8c4c7fbc57b08d379da5b1312690be04"
"99999ebcfdb78df077ad2727fd00969f"
"3097fca9b1ec8942c4305e550ef1b50a"
:ok

iex(27)> send shortener, {:get, "99999ebcfdb78df077ad2727fd00969f", self()}
{:get, "99999ebcfdb78df077ad2727fd00969f", #PID<0.102.0>}

iex(28)> flush
"https://google.com"
:ok

iex(29)> send shortener, {:get, "8c4c7fbc57b08d379da5b1312690be04", self()}
{:get, "8c4c7fbc57b08d379da5b1312690be04", #PID<0.102.0>}

iex(30)> flush
"https://ieftimov.com"
:ok

iex(31)> send shortener, {:get, "3097fca9b1ec8942c4305e550ef1b50a", self()}
{:get, "3097fca9b1ec8942c4305e550ef1b50a", #PID<0.102.0>}

iex(32)> flush
"https://github.com"
:ok
```

### Example with GenServer

```elixir
iex(1)> {:ok, pid} = UrlShortener.start_link(:foo)
{:ok, #PID<0.109.0>}

iex(2)> UrlShortener.shorten(:foo, "https://google.com")
"99999ebcfdb78df077ad2727fd00969f"

iex(3)> UrlShortener.get(:foo, "99999ebcfdb78df077ad2727fd00969f")
"https://google.com"

iex(4)> UrlShortener.stop(:foo)
:ok

iex(5)> Process.alive?(pid)
false
```

## Credits

[OTP in Elixir](https://ieftimov.com/post/otp-elixir-genserver-build-own-url-shortener/)

## About Me

[Magak Emmanuel](https://magak.me)

## License

[![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=for-the-badge)](#)

[![Open Source Love](https://badges.frapsoft.com/os/v2/open-source-200x33.png?v=103)](#)

Happy coding, Star before Fork 😊💪💯