file.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. // Read a file from disk, store it in Redis, then read it back from Redis.
  2. var redis = require("redis"),
  3. client = redis.createClient(),
  4. fs = require("fs"),
  5. filename = "kids_in_cart.jpg";
  6. // Get the file I use for testing like this:
  7. // curl http://ranney.com/kids_in_cart.jpg -o kids_in_cart.jpg
  8. // or just use your own file.
  9. // Read a file from fs, store it in Redis, get it back from Redis, write it back to fs.
  10. fs.readFile(filename, function (err, data) {
  11. if (err) throw err
  12. console.log("Read " + data.length + " bytes from filesystem.");
  13. client.set(filename, data, redis.print); // set entire file
  14. client.get(filename, function (err, reply) { // get entire file
  15. if (err) {
  16. console.log("Get error: " + err);
  17. } else {
  18. fs.writeFile("duplicate_" + filename, reply, function (err) {
  19. if (err) {
  20. console.log("Error on write: " + err)
  21. } else {
  22. console.log("File written.");
  23. }
  24. client.end();
  25. });
  26. }
  27. });
  28. });