-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add finish of node and python examples
- Loading branch information
Showing
7 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var redis = require("redis"); | ||
var client = redis.createClient(); | ||
var MongoClient = require('mongodb').MongoClient | ||
var url = 'mongodb://127.0.0.1:27017/test'; | ||
var assert = require('assert'); | ||
MongoClient.connect(url, function(err, db) { | ||
|
||
//ensure we've connected | ||
assert.equal(null, err); | ||
|
||
var crimes = db.collection('crimes'); | ||
|
||
//start timer | ||
console.time('query_time'); | ||
|
||
client.get('longquery_result', function(err,count){ | ||
|
||
if(err){ | ||
return console.error(err); | ||
} | ||
|
||
if(!count){ //cache miss! | ||
crimes.find({"Primary Type": "ROBBERY"}, function(err, data){ | ||
|
||
if(err){ | ||
return console.error(err); | ||
} | ||
|
||
data.count(function(err, count){ | ||
console.log('Total ROBBERY Crimes: ' + count); | ||
console.timeEnd('query_time'); | ||
|
||
//cache into redis | ||
client.set('longquery_result', count, function(err){ | ||
|
||
if(err){ | ||
return console.error(err); | ||
} | ||
|
||
//expire query in 20 seconds | ||
client.expire('longquery_result', 20); | ||
|
||
console.log('sucessfully cached query'); | ||
//close the database connection | ||
return db.close(); | ||
|
||
}); | ||
|
||
}); | ||
|
||
|
||
}); | ||
|
||
} | ||
else{ // cache hit!! | ||
console.log('Total ROBBERY Crimes: ' + count); | ||
console.timeEnd('query_time'); | ||
} | ||
}); | ||
|
||
|
||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var redis = require("redis"); | ||
var client = redis.createClient(6379, "someredis.redis.cache.windows.net", {auth_pass: 'PCeGeDZVrSQupyhx0Ce+oni6AnnQnjLsskJ6Rlo6Chk=', return_buffers: true}); | ||
var http = require('http'); | ||
http.createServer(function (req, res) { | ||
client.pfadd('clientips', req.connection.remoteAddress, function(err){ | ||
if(err){ | ||
return res.writeHead(500, {'Content-Type': 'text/plain'}); | ||
res.end('Error talking to redis ' + err + '\n'); | ||
} | ||
|
||
client.pfcount('clientips', function(err, count){ | ||
res.writeHead(500, {'Content-Type': 'text/plain'}); | ||
return res.end('Hello ' + req.connection.remoteAddress + '\n about ' + count + ' unique connections have visited this site!'); | ||
}); | ||
}); | ||
|
||
|
||
}).listen(process.env.PORT || 1337, '127.0.0.1'); | ||
console.log('Server running at http://127.0.0.1:' + (process.env.PORT || 1337)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "redis-node", | ||
"version": "1.0.0", | ||
"description": "simple redis example", | ||
"main": "app.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node count_server.js" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"mongodb": "^1.4.33", | ||
"redis": "^0.12.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var MongoClient = require('mongodb').MongoClient | ||
var url = 'mongodb://127.0.0.1:27017/test'; | ||
var assert = require('assert'); | ||
|
||
MongoClient.connect(url, function(err, db) { | ||
|
||
//ensure we've connected | ||
assert.equal(null, err); | ||
|
||
var crimes = db.collection('crimes'); | ||
//start timer | ||
console.time('query_time'); | ||
|
||
crimes.find({"Primary Type": "ROBBERY"}, function(err, data){ | ||
|
||
if(err){ | ||
return console.error(err); | ||
} | ||
|
||
data.count(function(err, count){ | ||
console.log('Total ROBBERY Crimes: ' + count); | ||
return console.timeEnd('query_time'); | ||
}); | ||
}); | ||
}); |
Submodule Start
added at
b9fb7c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import redis | ||
r = redis.StrictRedis(host="localhost", port=6379, db=0) | ||
r.hset("person:0", "first_name", "Rami") | ||
r.hset("person:0", "last_name", "Edouard") | ||
# now print the hset | ||
print r.hgetall("person:0") | ||
r.hmset("person:1", {"first_name":"Rami", "last_name":"Edouard", "location":"CANADA"}) | ||
print r.hgetall("person:1") | ||
|
||
# Sets | ||
print "Creating a set of countries in redis..." | ||
r.sadd("countries", "USA") | ||
r.sadd("countries", "Canada") | ||
r.sadd("countries", "Mexico") | ||
print "Getting contry set from redis..." | ||
print r.smembers("countries") |