Using Eredis, Redis With Erlang
http://no-fucking-idea.com/blog/2012/03/23/using-eredis-in-erlang/
Using Eredis, Redis With Erlang
MAR 23RD, 2012 | COMMENTS
Recently i decided to move my blog from tumblr.com
to octopress engine
because it is just easier for me to maintain it and it looks nicer. The old blog is under http://no-fucking-idea.tumblr.com. My first post on new blog is dedicated to using redis with erlang.
Eredis
Wooga created a really nice (performance driven) redis driver for erlang. You can get it here https://github.com/wooga/eredis. It is really easy and nice.
Initial sample
On project page you can find simple examples how to use eredis. Examples there are all you need (but i need something for front page of my new blog so i will rewrite them and explain them :) ).
First you need to start your eredis application
initialization
1 |
|
Client is the “connection / state” we will be using with rest of queries.
To query things with redis we will use q
method from eredis module which takes “Connection / Client” state and list with params. This api is very simple here are two most basic examples of get and set. GET:
get
1 |
|
and SET:
set
1 |
|
From my point of view this is ideal candidate to be turned into gen_server behavior. We will pass “Connection / Client” as state and also we will build some “key” serialization methods around it to make it more durable and make our life easy if we will decide to refactor it later on.
Free Api wrapper
First thing i saw during development using Erlang is that you get free api if you follow simple patterns and encapsulate things into gen_server’s and applications.
example_db.erl
1 |
|
Public Api
This code listing has two important parts first at top it starts at line 26. This is the public API which will be used by developer. This is this free api. Later on i will explain how to change redis to mongodb and probably to other db engines without doing changes in rest of our app. From my perspective this is awesome feature. In most cases when i had to make app scale problem of having code that was glues to one db engine was heavy.
eunit tests
At line 60. starts the declaration of tests, using rebar
and eunit
is very easy and it is always good to have test coverage in case of refactoring. I’m a fan of test driven development so i like to cover in tests first things that i will use or i think they might be error prone. Here is used “test generators” to just to show how to write tests for gen_server.
Rebar
Before i will explain more i need to say little about rebar
. It is a command line tool that was developed by basho
to help create app. it is by far the best thing i found learning erlang to help me do boring stuff and eliminate a lot of rage writing app.src
files. To get rebar simply do (you can always go to https://github.com/basho/rebar to get most up to date informations about building it)
1 |
|
I use my own set of zsh scripts so all i did to add it to my path was to edit .furby
file in my home dir. I strongly suggest also adding it to $PATH
just to make your life easier.
Back to example_db!
To create app using rebar you just need to
1 |
|
This command created src
folder with scaffold of application
OTP pattern and supervisor
thats almost all we need :). Now you can compile it using rebar compile
and run tests using rebar compile eunit
in out app currently we will see
rebar compile eunit
1 |
|
Nothing to do because its empty. Lets add our db module. But before this we need to add dependencies for eredis module. Lets create rebar.config
file and add it.
1 |
|
Now just run rebar get-deps
to get all dependencies downloaded. After adding our example_db.erl
into src
directory we can run rebar compile eunit
to compile and run tests. We have added {cover_enabled, true}
in rebar.conf so also test code coverage will be generated for us.
1 |
|
All seems to be fine! lets create file called start.sh
to test it out
1 |
|
and make it executable with chmod +x start.sh
And lets rock ‘n’ roll
1 |
|
Have fun :) Hope it was useful. You can download code for this blog post here https://github.com/JakubOboza/example_db-code
Huh ^___^
that was my first post on new blog ;)
Using Eredis, Redis With Erlang的更多相关文章
- Redis单台的安装部署及集群部署
Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集 合和有序集合.支持在服务器端计算集合的并,交和补集(diff ...
- 玩转spring boot——结合redis
一.准备工作 下载redis的windows版zip包:https://github.com/MSOpenTech/redis/releases 运行redis-server.exe程序 出现黑色窗口 ...
- 缓存、队列(Memcached、redis、RabbitMQ)
本章内容: Memcached 简介.安装.使用 Python 操作 Memcached 天生支持集群 redis 简介.安装.使用.实例 Python 操作 Redis String.Hash.Li ...
- [Erlang 0122] Erlang Resources 2014年1月~6月资讯合集
虽然忙,有些事还是要抽时间做; Erlang Resources 小站 2014年1月~6月资讯合集,方便检索. 小站地址: http://site.douban.com/204209/ ...
- Redis 与 数据库处理数据的两种模式
Redis 是一个高性能的key-value数据库. redis的出现,很大程度补偿了memcached这类key-value存储的不足,在部 分场合可以对关系数据库起到很好的补充作用.它提供了Pyt ...
- windows下redis安装
最近因公司项目原因,去了趟昆明出差,其中第一次接触安装redis,配置sentinel,学习到不少,但也都是皮毛而已,本随笔记下所学知识. 1.首先介绍下redis,来源自百度百科 redis是一个k ...
- mac下搭建redis环境
一.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合)和zset(有 ...
- Redis初识、设计思想与一些学习资源推荐
一.Redis简介 1.什么是Redis Redis 是一个开源的使用ANSI C 语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value 数据库,并提供多种语言的API.从2010 年 ...
- Python 【第六章】:Python操作 RabbitMQ、Redis、Memcache、SQLAlchemy
Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...
随机推荐
- 数值溢出(arithmetic overflow)问题与解决方案
0. 典型场景 两数相加(乘法).两数相减.一个数的阶乘,一个数的幂,这些统统可能造成数值的溢出: 避免数值溢出的方法: 当把一个计算出的很大的数赋值给一个 int(2^31-1)类型变量存储时,一般 ...
- Linux设备空间存储满问题
问题 linux创建文件夹文件.补全,启动服务均报错,具体报错信息如下 [root@localhost log]# mkdir /log/mysql -p mkdir: 无法创建目录"/lo ...
- .net core 修改网站启动端口
原文:.net core 修改网站启动端口 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/yenange/article/details/81675 ...
- 每日技术总结:Yarn和Npm大PK
今天想用npm安装vue-cli@2.9 npm install --global vue-cli@2.9 卡半天,安装不成功,清空缓存,换taobao源重来,还是一样. 无奈之下换yarn yarn ...
- MySQL參数binlog-do-db对binlogs写入的影响
1. 环境描写叙述 目的:当数据库中设置了binlog-do-db时.在不同的binlog_format=statement | row | mixed 下对binlog的写入影响,这个在主从复制中会 ...
- 获取iOS顶部状态栏和Navigation的高度
状态栏的高度 20 [[UIApplication sharedApplication] statusBarFrame].size.height Navigation的高度 44 self.navig ...
- bootstrap 时间控件带(时分秒)选择器(需要修改才能显示,请按照参数说明后面的步骤进行修改)
1.控件下载地址:http://www.bootcss.com/p/bootstrap-datetimepicker/index.htm,参数设置说明也在这个链接下面: 2.具体参数说明(复制原链接) ...
- Android onLoadFinished与onLoaderReset
onLoadFinished 这个方法是在前面已创建的加载器已经完成其加载过程后被调用,这个方法保证会在应用到加载器上的数据被释放之前被调用.在此方法中,你必须删除所有对旧数据的使用(因为它将很快会被 ...
- jQuery常用方法(持续更新) jQuery(转)
0.常用代码: 请容许我在1之前插入一个0,我觉得我有必要把最常用的代码放在第一位,毕竟大部分时间大家都是找代码的. (1)AJAX请求 $(function() { $('#send').click ...
- [Node] Run Any Version of a Node Tool with npx
As node projects evolve, new features are added all the time. This results in different errors or re ...