Giter Club home page Giter Club logo

as3redis's Introduction

Usage

First you create an instance of the main Redis connector class:

var redis:Redis = new Redis();

This initializes the connector, but does not connect to the server yet. The default parameters are the local machine and Redis' default port 6379. You might want to pass the hostname and port of your Redis instance in the constructor, in case they are different from the default. Note that it's highly discouraged to connect to a production server (or anything outside your local network really) due to security issues.

Connecting to a server

You don't need to explicitly connect to the server, as3redis does that for you behind the scenes. You can do that, though, if you need to:

var redis:Redis = new Redis();
redis.addEventListener(Event.CONNECT, connectHandler);
redis.connect();

Authentication

Redis allows for simple authentication through the AUTH command. If you set up your Redis instance to require a password, just set the password property before the server connects:

var redis:Redis = new Redis();
redis.password = "joshua";

Now redis will transparently authenticate right after it connects.

Sending commands

It is almost trivial to send commands to your Redis instance, using the sendXXX() methods:

var redis:Redis = new Redis();
redis.password = "joshua";
redis.sendINFO();
redis.sendPING();
redis.sendECHO("Hello World");
redis.sendSET("key", "value");
redis.sendGET("key");

This will auto-connect to your Redis instance, authenticate the client with the provided password and bulk-send the commands INFO, PING, ECHO, SET and GET.

Receiving responses

In Flash, pretty much all native networking APIs are asynchronous, and so is as3redis. You can bulk-send commands to Redis, and as3swf always keeps the commands in sync with the received responses.

To get notified by as3redis when a response was received for a specific command, add one or more responders to the command. The simplest example:

var redis:Redis = new Redis();
redis.sendINFO().addSimpleResponder(
	function(cmd:INFO):void {
		trace(cmd);
	}
);

// Trace output:
// [INFO]
//   Role: master
//   RedisVersion: 1.02
//   UsedMemory: 963206
//   UptimeInSeconds: 1076279
//   UptimeInDays: 12
//   ConnectedClients: 1
//   ConnectedSlaves: 0
//   LastSaveTime: Mon Nov 16 10:58:35 GMT-0200 2009
//   ChangesSinceLastSave: 0
//   BGSaveInProgress: 0
//   TotalConnectionsReceived: 432
//   TotalCommandsProcessed: 27770	

You can (and should) also catch error responses:

var redis:Redis = new Redis();
redis.sendINFO().addSimpleResponder(
	function(cmd:INFO):void {
		trace("OK!", cmd);
	},
	function(cmd:INFO):void {
		trace("ERROR!", cmd);
	}
);

Or use one responder for all commands:

var redis:Redis = new Redis();
var responder:RedisResponder = new RedisResponder(success, fault);

redis.sendSETNX("key", "value").addResponder(responder);
redis.sendGET("key").addResponder(responder);

function success(cmd:RedisCommand):void {
	trace(cmd);
}
function fault(cmd:RedisCommand):void {
	trace("ERROR!\r" + cmd);
}

// Trace output:
// [SETNX key value]
//   1
// [GET key]
//   0: value

as3redis's People

Contributors

claus avatar

Stargazers

Francis Varga avatar

Watchers

Francis Varga avatar James Cloos avatar

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.