Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

97 lines (97 sloc) 2.211 kb
{
"metadata": {
"name": "redis - ex 3 (time series)"
},
"nbformat": 2,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import redis",
"import time",
"",
"def now():",
" return int(round(time.time()))",
"",
"# Creating a connection to redis",
"db = redis.Redis(db=3)",
"# WARNING: remove all key/values in the current database",
"db.flushdb()"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 18,
"text": [
"True"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"start = now()",
"# We keep (assumed to be unique) timestamp-value pairs in sorted arrays",
"# Having timestamps in the score allows for fast range get (insert in Log(N))",
"# BEWARE: zadd order different from redis command zadd ! in python, zadd(key_name, value, score)",
"db.zadd(\"event\", 11, now())",
"time.sleep(1)",
"db.zadd(\"event\", 13, now())",
"time.sleep(1)",
"db.zadd(\"event\", 15, now())",
"time.sleep(1)",
"db.zadd(\"event\", 14, now())",
"time.sleep(1)",
"db.zadd(\"event\", 12, now())",
"end = now()",
"print start, end"
],
"language": "python",
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1330757498 1330757502"
]
}
],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"db.zrangebyscore(\"event\", start+2, end)"
],
"language": "python",
"outputs": [
{
"output_type": "pyout",
"prompt_number": 23,
"text": [
"['15', '14', '12']"
]
}
],
"prompt_number": 23
},
{
"cell_type": "code",
"collapsed": true,
"input": [],
"language": "python",
"outputs": [],
"prompt_number": " "
}
]
}
]
}
Jump to Line
Something went wrong with that request. Please try again.