xrtlibrary-sessiondeprecated

Session module of XRT library.

Usage no npm install needed!

<script type="module">
  import xrtlibrarySession from 'https://cdn.skypack.dev/xrtlibrary-session';
</script>

README

Read Me

Introduction

This package is a session-like integrated key-value(K-V) database (a part of XRT library).

Installation

To install this package, type following command in your terminal:

npm install xrtlibrary-session --save

And then, you can import this package in your NodeJS environment with following "require" statement.

var XRTLibSession = require("xrtlibrary-session");

API (Usage)

SessionKeyAllocator (Class):

+---------------------------------------+--------------------------+ | Method | Description | +---------------------------------------+--------------------------+ | constructor([minLength], [charTable]) | Construct the allocator. | +---------------------------------------+--------------------------+ | allocate() | Allocate a key. | +---------------------------------------+--------------------------+ | deallocate([key]) | Deallocate a key. | +---------------------------------------+--------------------------+

SessionManager (Class):

+----------------------------------------------+--------------------------------------------+ | Method | Description | +----------------------------------------------+--------------------------------------------+ | constructor() | (No parameter required.) | +----------------------------------------------+--------------------------------------------+ | set([key], [value], [hasTimeout], [timeout]) | Set a K-V pair. | +----------------------------------------------+--------------------------------------------+ | get([key]) | Get the value mapped to a specified key. | +----------------------------------------------+--------------------------------------------+ | has([key]) | Get whether a key exists. | +----------------------------------------------+--------------------------------------------+ | renew([key]) | Renew a K-V pair (if the K-V pair has | | | timeout limit). | +----------------------------------------------+--------------------------------------------+ | delete([key]) | Delete a K-V pair. | +----------------------------------------------+--------------------------------------------+ | tick() | Give the manager a tick to collect expired | | | K-V pairs. | +----------------------------------------------+--------------------------------------------+ | iterate() | Get all K-V pairs. | +----------------------------------------------+--------------------------------------------+ | getLength() | Get the count of K-V pairs. | +----------------------------------------------+--------------------------------------------+

Example

Code:

var keyAllocator = new XRTLibSession.SessionKeyAllocator(8, "0123456789abcdef"); var sessionMGR = new XRTLibSession.SessionManager(); sessionMGR.set(keyAllocator.allocate(), "Client 1", true, 2500); sessionMGR.set(keyAllocator.allocate(), "Client 2", true, 5000); console.log("Session set."); var timer = setInterval(function() { var expired = sessionMGR.tick(); for (var i = 0; i < expired.length; ++i) { var key = expired[i][0]; var value = expired[i][1]; console.log("Session expired: " + key + " => " + value + "."); keyAllocator.deallocate(key); } if (sessionMGR.getLength() == 0) { clearInterval(timer); } }, 100);

Output (sample):

  • Session set.
  • Session expired: 23ce983e => Client 1.
  • Session expired: 948f5361 => Client 2.