LearnGenserver
Learn GenServer by Building Your Own URL Shortener
Running
Example
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
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
About Me
License
Happy coding, Star before Fork 😊💪💯