Advertisement
Guest User

Untitled

a guest
Jan 26th, 2015
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. // Redis server analogy for programmers
  2.  
  3. function redisServer(id, srv){
  4. this.upvotes = 0;
  5. this.id = id;
  6. this.central = srv;
  7. return this;
  8. }
  9. redisServer.prototype.resetUpvotes = function(){
  10. this.upvotes = 0;
  11. console.log("redisServer #" + this.id + " flushed it's cache");
  12. return this;
  13. }
  14. redisServer.prototype.addUpvote = function(){
  15. this.upvotes++;
  16. console.log("redisServer #" + this.id + " got an upvote!");
  17. return this;
  18. }
  19. redisServer.prototype.sendUpvotes = function(){
  20. this.central.upvotes += this.upvotes;
  21. console.log("redisServer #" + this.id + " sent it's upvotes[" + this.upvotes + "] to central");
  22. return this;
  23. }
  24.  
  25. function dummyUser(srv){
  26. this.srv = srv[Math.floor(Math.random() * srv.length)];
  27. console.log("dummyUser has joined server " + this.srv.id);
  28. return this;
  29. };
  30. dummyUser.prototype.requestImage = function(){
  31. console.log("dummyUser on server " + this.srv.id + " has requested an image and got " + central_server.serveImage());
  32. return this;
  33. };
  34. dummyUser.prototype.browseImgur = function(){
  35. var self = this;
  36. this.interval = setInterval(function(){
  37. if(Math.random() > 0.9){
  38. self.srv.addUpvote();
  39. console.log("dummyUser liked an image and upvoted it on redisServer #" + self.srv.id);
  40. };
  41. if(Math.random() > 0.9){
  42. self.requestImage();
  43. };
  44. }, 1000);
  45. return this;
  46. };
  47.  
  48. function centralServer(){
  49. this.upvotes = 0;
  50. this.image = ["cat.gif", "dickbutt.gif", "doge.gif"];
  51. return this;
  52. }
  53. centralServer.prototype.collectUpvotes = function(){
  54. for(i = redis_servers.length; i--;){
  55. redis_servers[i].sendUpvotes().resetUpvotes();
  56. }
  57. return this;
  58. }
  59. centralServer.prototype.serveImage = function(){
  60. return this.image[Math.round(Math.random() * this.image.length)];
  61. }
  62. centralServer.prototype.listenRedis = function(){
  63. var self = this;
  64. this.interval = setInterval(function(){
  65. self.collectUpvotes();
  66. }, 10000);
  67. return this;
  68. }
  69.  
  70. var redis_servers = []
  71. , dummy_users = []
  72. , central_server = new centralServer().listenRedis();
  73. for(i = 20; i--;){ // building 20 servers
  74. redis_servers[i] = new redisServer(i, central_server);
  75. };
  76. for(i = 20; i--;){ // building 100 dummy users
  77. dummy_users[i] = new dummyUser(redis_servers).browseImgur();
  78. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement