Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Redis server analogy for programmers
- function redisServer(id, srv){
- this.upvotes = 0;
- this.id = id;
- this.central = srv;
- return this;
- }
- redisServer.prototype.resetUpvotes = function(){
- this.upvotes = 0;
- console.log("redisServer #" + this.id + " flushed it's cache");
- return this;
- }
- redisServer.prototype.addUpvote = function(){
- this.upvotes++;
- console.log("redisServer #" + this.id + " got an upvote!");
- return this;
- }
- redisServer.prototype.sendUpvotes = function(){
- this.central.upvotes += this.upvotes;
- console.log("redisServer #" + this.id + " sent it's upvotes[" + this.upvotes + "] to central");
- return this;
- }
- function dummyUser(srv){
- this.srv = srv[Math.floor(Math.random() * srv.length)];
- console.log("dummyUser has joined server " + this.srv.id);
- return this;
- };
- dummyUser.prototype.requestImage = function(){
- console.log("dummyUser on server " + this.srv.id + " has requested an image and got " + central_server.serveImage());
- return this;
- };
- dummyUser.prototype.browseImgur = function(){
- var self = this;
- this.interval = setInterval(function(){
- if(Math.random() > 0.9){
- self.srv.addUpvote();
- console.log("dummyUser liked an image and upvoted it on redisServer #" + self.srv.id);
- };
- if(Math.random() > 0.9){
- self.requestImage();
- };
- }, 1000);
- return this;
- };
- function centralServer(){
- this.upvotes = 0;
- this.image = ["cat.gif", "dickbutt.gif", "doge.gif"];
- return this;
- }
- centralServer.prototype.collectUpvotes = function(){
- for(i = redis_servers.length; i--;){
- redis_servers[i].sendUpvotes().resetUpvotes();
- }
- return this;
- }
- centralServer.prototype.serveImage = function(){
- return this.image[Math.round(Math.random() * this.image.length)];
- }
- centralServer.prototype.listenRedis = function(){
- var self = this;
- this.interval = setInterval(function(){
- self.collectUpvotes();
- }, 10000);
- return this;
- }
- var redis_servers = []
- , dummy_users = []
- , central_server = new centralServer().listenRedis();
- for(i = 20; i--;){ // building 20 servers
- redis_servers[i] = new redisServer(i, central_server);
- };
- for(i = 20; i--;){ // building 100 dummy users
- dummy_users[i] = new dummyUser(redis_servers).browseImgur();
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement