Giter Site home page Giter Site logo

kraken_example_session_redis's Introduction

Kraken_Example_Session_Redis

An example of the use of Redis for session store

Introduction

In this example we're going to use Redis to store the session keys instead of the MemoryStore, which is the default behavior on a fresh install of a kraken application.

The topics covered here are:

  • Installing Redis on Ubuntu
  • Using the generator to build an application
  • Installing the modules needed for Redis
  • Configuration of the middleware for Redis

Prerequisites

  • Ubuntu 13.10
  • You will --of course-- need Node (Version >= 0.10.20 preferred)
  • The Kraken generator. If you havent yet installed it, simply do: sudo npm install -g generator-kraken

Install Redis

Ubuntu 13.10 provides Redis 2.6.13 on their repository, so just need to do: sudo apt-get install redis-server. After installing, you will have a running redis server on the default port and with trusted access. For more information about installing and configuring Redis, please go to the Redis website.

Create an application

$ yo kraken

     ,'""`.
    / _  _ \
    |(@)(@)|   Release the Kraken!
    )  __  (
   /,'))((`.\
  (( ((  )) ))
   `\ `)(' /'

[?] Application name: Kraken_Example_Session_Redis
[?] Description: An example of using Redis to store session data
[?] Author: AlexSantos
[?] Use RequireJS? (Y/n) n

The generator will set up the app and install the dependencies. After it's done, just go into the newly created directory

$ cd Kraken_Example_Session_Redis

Installing the modules needed

To use Redis, it is necessary to install connect-redis.

$ npm install --save connect-redis@~1.4.6

After issuing this command, the package.json file has one more dependency - "connect-redis": "~1.4.6"

Configuring the middleware.json file

To instruct kraken to use Redis as the session store, the configuration must be changed to:

In middleware.json

      "session": {
          "module": "connect-redis",
          "secret": "80bc6d67f80813ccc78ff77adf0eefcafa7eeef6"
      }

To pass options to connect-redis, just add a config object:

      "session": {
          "module": "connect-redis",
          "config": {
            "ttl": 3600
          },
          "secret": "300b6574099026c0e2c46130fb62531d2e7ff6e1"
      }

And you're done!

After this, just issue: $ npm start and your application is using Redis!

Notes

Redis client: on a terminal, issue: $ redis-cli and you'll have a command line for accessing Redis. Try

$ redis-cli
redis 127.0.0.1:6379> keys *
1) "sess:Ot9K7wROHZHnr8oUnwkUteds"
2) "sess:faUzznKZ3JPNE07jL3U7cfm2"
3) "sess:XlIwce1sNO9ezHzXnhdaq7B8"
4) "sess:KNYgBRYKfjwbPkzSRSgpWKpt"
5) "sess:GZ92gMro02ynrGIkTgJiuXlC"
redis 127.0.0.1:6379> 

and you'll have a list of the keys stored.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.