Skip to content

Commit

Permalink
add finish of node and python examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sedouard committed Mar 9, 2015
1 parent d6d1ff4 commit b0ff23f
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 0 deletions.
Binary file not shown.
63 changes: 63 additions & 0 deletions module5_redis_python_node/node/Finish/cached_query.js
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');
}
});



});
19 changes: 19 additions & 0 deletions module5_redis_python_node/node/Finish/count_server.js
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));
16 changes: 16 additions & 0 deletions module5_redis_python_node/node/Finish/package.json
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"
}
}
25 changes: 25 additions & 0 deletions module5_redis_python_node/node/Finish/query.js
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');
});
});
});
1 change: 1 addition & 0 deletions module5_redis_python_node/node/Start
Submodule Start added at b9fb7c
16 changes: 16 additions & 0 deletions module5_redis_python_node/python/End/datastructures.py
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")

0 comments on commit b0ff23f

Please sign in to comment.