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
- Host: GitHub
- URL: https://github.com/manuelgeek/learn-genserver
- Owner: manuelgeek
- Created: 2020-07-29T19:32:01.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-29T19:32:33.000Z (almost 5 years ago)
- Last Synced: 2025-01-17T14:57:30.608Z (4 months ago)
- Topics: elixir-lang, genserver, otp
- Language: Elixir
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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"
:okiex(27)> send shortener, {:get, "99999ebcfdb78df077ad2727fd00969f", self()}
{:get, "99999ebcfdb78df077ad2727fd00969f", #PID<0.102.0>}iex(28)> flush
"https://google.com"
:okiex(29)> send shortener, {:get, "8c4c7fbc57b08d379da5b1312690be04", self()}
{:get, "8c4c7fbc57b08d379da5b1312690be04", #PID<0.102.0>}iex(30)> flush
"https://ieftimov.com"
:okiex(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)
:okiex(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
[](#)
[](#)
Happy coding, Star before Fork 😊💪💯