HEXH's Blog

面朝大海,春暖花开


  • 首页

  • 分类

  • 标签

  • 归档

  • 公益404

redis-eval

发表于 2015-07-03   |   分类于 redis   |  

EVAL script numkeys key [key ...] arg [arg ...]

Introduction to EVAL

EVAL and EVALSHA are used to evaluate scripts using the Lua interpreter built into Redis starting from version 2.6.0.
The first argument of EVAL is a Lua 5.1 script. The script does not need to define a Lua function (and should not). It is just a Lua program that will run in the context of the Redis server.
The second argument of EVAL is the number of arguments that follows the script (starting from the third argument) that represent Redis key names. This arguments can be accessed by Lua using the KEYS global variable in the form of a one-based array (so KEYS[1], KEYS[2], ...).
All the additional arguments should not represent key names and can be accessed by Lua using the ARGV global variable, very similarly to what happens with keys (so ARGV[1], ARGV[2], ...).
The following example should clarify what stated above:
> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second
1) "key1"
2) "key2"
3) "first"
4) "second"

jedis
Object result = jedis.eval(
"return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}", 2, "key1",
"key2", "first", "second");
LOG.info("result class: " + result.getClass().toString());
LOG.info("result context:" + Joiner.on(",").join((List) result));
[INFO ] 15:52:19,188  result class: class java.util.ArrayList
[INFO ] 15:52:19,194  result context:key1,key2,first,second

Note: as you can see Lua arrays are returned as Redis multi bulk replies, that is a Redis return type that your client library will likely convert into an Array type in your programming language.
It is possible to call Redis commands from a Lua script using two different Lua functions:

  • redis.call()
  • redis.pcall()

redis.call() is similar to redis.pcall(), the only difference is that if a Redis command call will result in an error, redis.call() will raise a Lua error that in turn will force EVAL to return an error to the command caller, while redis.pcall will trap the error and return a Lua table representing the error.
The arguments of the redis.call() and redis.pcall() functions are all the arguments of a well formed Redis command:
> eval "return redis.call('set','foo','bar')" 0
OK

The above script sets the key foo to the string bar. However it violates the EVAL command semantics as all the keys that the script uses should be passed using the KEYS array:
> eval "return redis.call('set',KEYS[1],'bar')" 1 foo
OK

All Redis commands must be analyzed before execution to determine which keys the command will operate on. In order for this to be true for EVAL, keys must be passed explicitly. This is useful in many ways, but especially to make sure Redis Cluster can forward your request to the appropriate cluster node.
Note this rule is not enforced in order to provide the user with opportunities to abuse the Redis single instance configuration, at the cost of writing scripts not compatible with Redis Cluster.
Lua scripts can return a value that is converted from the Lua type to the Redis protocol using a set of conversion rules.

阅读全文 »

redis-transactions

发表于 2015-07-03   |   分类于 redis   |  

MULTI, EXEC, DISCARD and WATCH are the foundation of transactions in Redis. They allow the execution of a group of commands in a single step, with two important guarantees:

  • All the commands in a transaction are serialized and executed sequentially. It can never happen that a request issued by another client is served in the middle of the execution of a Redis transaction. This guarantees that the commands are executed as a single isolated operation.
  • Either all of the commands or none are processed, so a Redis transaction is also atomic. The EXEC command triggers the execution of all the commands in the transaction, so if a client loses the connection to the server in the context of a transaction before calling the MULTI command none of the operations are performed, instead if the EXEC command is called, all the operations are performed. When using the append-only file Redis makes sure to use a single write(2) syscall to write the transaction on disk. However if the Redis server crashes or is killed by the system administrator in some hard way it is possible that only a partial number of operations are registered. Redis will detect this condition at restart, and will exit with an error. Using the redis-check-aof tool it is possible to fix the append only file that will remove the partial transaction so that the server can start again.

Starting with version 2.2, Redis allows for an extra guarantee to the above two, in the form of optimistic locking in a way very similar to a check-and-set (CAS) operation. This is documented later on this page.

Usage

A Redis transaction is entered using the MULTI command. The command always replies with OK. At this point the user can issue multiple commands. Instead of executing these commands, Redis will queue them. All the commands are executed once EXEC is called.
Calling DISCARD instead will flush the transaction queue and will exit the transaction.
The following example increments keys foo and bar atomically.
> MULTI
OK
> INCR foo
QUEUED
> INCR bar
QUEUED
> EXEC
1) (integer) 1
2) (integer) 1

As it is possible to see from the session above, EXEC returns an array of replies, where every element is the reply of a single command in the transaction, in the same order the commands were issued.
When a Redis connection is in the context of a MULTI request, all commands will reply with the string QUEUED (sent as a Status Reply from the point of view of the Redis protocol). A queued command is simply scheduled for execution when EXEC is called.

阅读全文 »

Request/Response protocols and RTT

发表于 2015-07-02   |   分类于 redis   |  

Redis is a TCP server using the client-server model and what is called a Request/Response protocol.
This means that usually a request is accomplished with the following steps:

  • The client sends a query to the server, and reads from the socket, usually in a blocking way, for the server response.
  • The server processes the command and sends the response back to the client.

So for instance a four commands sequence is something like this:

  • Client: INCR X
  • Server: 1
  • Client: INCR X
  • Server: 2
  • Client: INCR X
  • Server: 3
  • Client: INCR X
  • Server: 4

Clients and Servers are connected via a networking link. Such a link can be very fast ([a loopback interface][lookback interface]) or very slow (a connection established over the Internet with many hops between the two hosts). Whatever the network latency(通信延迟) is, there is a time for the packets to travel from the client to the server, and back from the server to the client to carry the reply.
This time is called RTT (Round Trip Time-往返时间). It is very easy to see how this can affect the performances when a client needs to perform many requests in a row (for instance adding many elements to the same list, or populating a database with many keys). For instance if the RTT time is 250 milliseconds (in the case of a very slow link over the Internet), even if the server is able to process 100k requests per second, we'll be able to process at max four requests per second.
If the interface used is a loopback interface, the RTT is much shorter (for instance my host reports 0,044 milliseconds pinging 127.0.0.1), but it is still a lot if you need to perform many writes in a row.
Fortunately there is a way to improve this use case.

Redis Pipelining

A Request/Response server can be implemented so that it is able to process new requests even if the client didn't already read the old responses. This way it is possible to send multiple commands to the server without waiting for the replies at all, and finally read the replies in a single step.

阅读全文 »

redis-conf

发表于 2015-06-30   |   分类于 redis   |  

Redis is able to start without a configuration file using a built-in default configuration, however this setup is only recommended for testing and development purposes.
The proper way to configure Redis is by providing a Redis configuration file, usually called redis.conf.
The redis.conf file contains a number of directives that have a very simple format:keyword argument1 argument2 ... argumentN
It is possible to provide strings containing spaces as arguments using quotes, as in the following example:requirepass "hello world"

Passing arguments via the command line

Since Redis 2.6 it is possible to also pass Redis configuration parameters using the command line directly. This is very useful for testing purposes. The following is an example that starts a new Redis instance using port 6380 as a slave of the instance running at 127.0.0.1 port 6379.

./redis-server --port 6380 --slaveof 127.0.0.1 6379

Changing Redis configuration while the server is running

It is possible to reconfigure Redis on the fly without stopping and restarting the service, or querying the current configuration programmatically using the special commands CONFIG SET and CONFIG GET
Not all the configuration directives are supported in this way, but most are supported as expected. Please refer to the CONFIG SET and CONFIG GET pages for more information.
Note that modifying the configuration on the fly has no effects on the redis.conf file so at the next restart of Redis the old configuration will be used instead.
Make sure to also modify the redis.conf file accordingly to the configuration you set using CONFIG SET. There are plans to provide a CONFIG REWRITE command that will be able to run the redis.conf file rewriting the configuration accordingly to the current server configuration, without modifying the comments and the structure of the current file.

阅读全文 »

ssh behind socks 5

发表于 2015-06-30   |   分类于 linux   |  

nc

ssh -o 'ProxyCommand nc -x xxx.xxx.xxx.xxx:1080 -X 5 -Pruser %h %p' root@xxx.xxx.xxx.xxx

-X 5 socks version 5
-P ruser proxy username
-x proxy_host|proxy:port

nc help1
nc help2

报错nc: authentication method negotiation failed
未找到原因

corkscrew

corkscrew只支持http(s),未尝试。

阅读全文 »

Clojure 编程-读书笔记1(进入Clojure仙境)

发表于 2015-06-27   |   分类于 clojure   |  

列表 在书中一般标识函数调用,而不是数据结构。刚开始没注意,看的很混乱。

为什么选择Clojure?

  • Clojure是运行在JVM上的一种语言

    Clojure代码可以使用任何java类库,反之Clojure库也可以被任何Java代码使用
    ?和原生的java类库的差别?

  • Clojure是Lisp

  • Clojure是函数式编程语言

    Clojure鼓励使用高阶函数,并且提供了一些高效、不可变的数据结构,避免由于状态的不加控制的修改而导致的一些bug,并且也使Clojure成为一种进行并行、并发编程的完美语言。
    ?java并发编程是需要状态控制的,而clojure可以直接调用java代码?

  • clojure提供一种进行并行、并发编程的创新式解决方案

    clojure的引用类型强制我们把对象的状态和对象的标识区分开。?
    clojure对于多线程编程的支持使得我们不用手动加锁、解锁也能编写多线程的代码。

  • clojure是一种动态的编程语言

    clojure是动态的同时也是强类型的(和Python、Ruby类似)
    clojure支持运行时更新现有代码。?

阅读全文 »

English-Words-20160415

发表于 2015-03-15   |   分类于 english   |  
  • canonical /kə'nɒnɪk(ə)l/ adj. 依教规的;权威的;牧师的; n. 牧师礼服 adj. 标准的
  • italic /ɪ'tælɪk/ n. 斜体字(或字母、数码等); adj. [印刷] 斜体的
  • paragraph /'pærəgrɑːf/ n. 段落;短评;段落符号; vt. 将…分段
  • alternate /'ɔːltəneɪt; 'ɒl-/ vi. 交替;轮流; vt. 使交替;使轮流; adj. 交替的;轮流的; n. 替换物
  • typography /taɪ'pɒgrəfɪ/ n. 排印;[印刷] 活版印刷术;印刷格式
  • palette /'pælɪt/ n. 调色板;颜料; n. (Palette)人名;(法)帕莱特

Skip-List-Cookbook

发表于 2015-03-14   |   分类于 english   |  
  • probabilistic /,prɒbəbɪ'lɪstɪk/ adj. 概率性的;或然说的,盖然论的
  • supplant /sə'plɑːnt/ vt. 代替;排挤掉
  • algorithm /'ælgərɪð(ə)m/ n. [计][数] 算法,运算法则
  • asymptotic /,æsɪmp'tɒtɪk/ adj. 渐近的;渐近线的
  • versatile /'vɜːsətaɪl/ adj. 多才多艺的;通用的,万能的;多面手的
  • substitute /'sʌbstɪtjuːt/ n. 代用品;代替者; vi. 替代; vt. 代替
  • equivalent /ɪ'kwɪv(ə)l(ə)nt/ adj. 等价的,相等的;同意义的; n. 等价物,相等物
  • significantly /sɪg'nɪfɪk(ə)ntlɪ/ adv. 意味深长地;值得注目地
  • degenerate /dɪ'dʒen(ə)rət/ vt. 使退化;恶化; vi. 退化;堕落; adj. 退化的;堕落的; n. 堕落的人
  • permute /pə'mjuːt/ vt. 交换;变更;排列
  • impractical /ɪm'præktɪk(ə)l/ adj. 不切实际的,不现实的;不能实行的
  • consult /kən'sʌlt/ vt. 查阅;商量;向…请教; vi. 请教;商议;当顾问
  • pivot /'pɪvət/ n. 枢轴;中心点;旋转运动; vt. 以…为中心旋转;把…置于枢轴上; vi. 在枢轴上转动;随…转移; adj. 枢轴的;关键的; n. (Pivot)人名;(德)皮福特;(法)皮沃
  • allocate /'æləkeɪt/ vt. 分配;拨出;使坐落于; vi. 分配;指定
  • arrange /ə'reɪn(d)ʒ/ vt. 安排;排列;整理; vi. 安排;排列;协商
  • assure /ə'ʃʊə; ə'ʃɔː/ vt. 保证;担保;使确信;弄清楚
  • sequence /'siːkw(ə)ns/ n. [数][计] 序列;顺序;续发事件; vt. 按顺序排好
  • maintain /meɪn'teɪn; mən'teɪn/ vt. 维持;继续;维修;主张;供养
  • intermediate /,ɪntə'miːdɪət/ vi. 起媒介作用; adj. 中间的,中级的; n. [化学] 中间物;媒介
  • desire /dɪ'zaɪə/ n. 欲望;要求,心愿;性欲; vt. 想要;要求;希望得到…; vi. 渴望; n. (Desire)人名;(刚(布)、英)德西雷
  • regard /rɪ'gɑːd/ n. 注意;尊重;问候;凝视; vt. 注重,考虑;看待;尊敬;把…看作;与…有关; vi. 注意,注重;注视; n. (Regard)人名;(西、意)雷加德;(法)勒加尔
  • capped // v. 给…戴帽;去蒂;覆以…;除去盖子;胜过(cap的过去分词形式); adj. 包过的;加盖的;去蒂的;[计]被限制在...
  • overshoot /əʊvə'ʃuːt/ vt. 超越;打过头;把…做过头; vi. 射击越标;(飞机)滑出跑道;行动过火; n. 超越目标;行动过火
  • splice /splaɪs/ vt. 拼接;接合;使结婚; n. 接合;结婚
  • portion /'pɔːʃ(ə)n/ n. 部分;一份;命运; vt. 分配;给…嫁妆
  • denote /dɪ'nəʊt/ vt. 表示,指示
  • ideally /aɪ'dɪəl(l)ɪ; aɪ'diːəl(l)ɪ/ adv. 理想地;观念上地
  • formula /'fɔːmjʊlə/ n. [数] 公式,准则;配方;婴儿食品
  • frequently /'friːkw(ə)ntlɪ/ adv. 频繁地,经常地;时常,屡次
  • complicate /'kɒmplɪkeɪt/ vt. 使复杂化;使恶化;使卷入
  • noticeably /'notɪsəbli/ adv. 显著地,明显地;引人注目地
  • utilize /'jutəlaɪz/ vt. 利用
  • dice /daɪs/ vt. 切成方块; n. 骰子; vi. 掷骰子; n. (Dice)人名;(英)戴斯
  • purist /'pjʊərɪst/ n. 纯化论者,力求纯化的人
  • intuitively /ɪn'tjʊɪtɪvli/ adv. 直观地;直觉地
  • pessimistic /,pesɪ'mɪstɪk/ adj. 悲观的,厌世的;悲观主义的

English-Words-20160220

发表于 2015-02-20   |   分类于 english   |  
  • principal /'prɪnsəp(ə)l/ adj. 主要的;资本的; n. 首长;校长;资本;当事人
  • dereference /,di'rɛfrəns/ n. 废弃;解除参照
  • circuit /'sɜːkɪt/ n. [电子] 电路,回路;巡回;一圈;环道; vi. 环行; vt. 绕回…环行
  • decompose /diːkəm'pəʊz/ vi. 分解;使腐烂; vt. 分解;腐烂
  • individually /ɪndɪ'vɪdjʊ(ə)lɪ/ adv. 个别地,单独地
  • disjunction /dɪs'dʒʌŋ(k)ʃ(ə)n/ n. 分离;析取;分裂;折断
  • conjunction /kən'dʒʌŋ(k)ʃ(ə)n/ n. 结合;[语] 连接词;同时发生
  • demonstrate /'demənstreɪt/ vt. 证明;展示;论证; vi. 示威
  • generic /dʒɪ'nerɪk/ adj. 类的;一般的;属的;非商标的
  • variance /'veərɪəns/ n. 变异;变化;不一致;分歧;[数] 方差
  • caveat /'kævɪæt; 'keɪ-/ n. 警告;中止诉讼手续的申请;货物出门概不退换;停止支付的广告
  • asymptotically // adv. 渐近地
  • asymptotic /,æsɪmp'tɒtɪk/ adj. 渐近的;渐近线的

English-Words-20160215

发表于 2015-02-15   |   分类于 english   |  
  • homogeneous /,hɒmə(ʊ)'dʒiːnɪəs; -'dʒen-/ adj. 均匀的;[数] 齐次的;同种的;同类的
  • heterogeneous /,het(ə)rə(ʊ)'dʒiːnɪəs; -'dʒen-/ adj. [化学] 多相的;异种的;异类的;[化学] 不均匀的;由不同成分形成的
  • covariant /'ko'vɛrɪənt/ adj. 协变的; n. 共变式;[数][物] 协变量
  • in terms of // 依据;按照;在…方面;以…措词
  • instructive /ɪn'strʌktɪv/ adj. 有益的;教育性的
  • assume /ə'sjuːm/ vt. 承担;假定;采取;呈现; vi. 装腔作势;多管闲事
  • trivial /'trɪvɪəl/ adj. 不重要的,琐碎的;琐细的
  • traverse /'trævəs; trə'vɜːs/ n. 穿过;横贯;横木; vt. 穿过;反对;详细研究;在…来回移动; vi. 横越;旋转;来回移动; adj. 横贯的; n. (Traverse)人名;(英)特拉弗斯;(法)特拉韦尔斯
  • proportional /prə'pɔːʃ(ə)n(ə)l/ adj. 比例的,成比例的;相称的,均衡的; n. [数] 比例项
  • dual /'djuːəl/ adj. 双的;双重的; n. 双数;双数词; n. (Dual)人名;(法)迪阿尔
阅读全文 »
1…345…9
Xuehui He

Xuehui He

面朝大海,春暖花开

83 日志
23 分类
13 标签
RSS
github
© 2013 - 2016 Xuehui He
由 Hexo 强力驱动
主题 - NexT.Mist