`
liuxinglanyue
  • 浏览: 547569 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

JRedisQuickStart

阅读更多

 JRedisQuickStart  

#Get going with JRedis - here's how:

Introduction

So how do you use JRedis? Pull the code (r16) or download from github.

Details

JRedis is a specification and a reference implementation. Currently there is one implementation providing (blocking semantics on method calls) for a passive client (that uses the caller's thread to do its job).

This initial client can not be shared across threads, but you can certainly either put it behind a synchronized gate, or, fire up a whole bunch for each one of your threads, as you prefer.

If you do share an instance from behind a facade, do note that redis connections are stateful, and if you plan on using the facility to switch between dbs using jredis.select(db) SELECT db it is almost guaranteed to be bad idea to share a single connection across multiple threads. However, if you will not be using select, then there should be no problems with sharing a single connection from behind a facade. If switching dbs is required, then you will need to create dedicated JRedis instances per thread. (This is very much a server issue and not JRedis specific.)

Alright, that said, here is a barebones JRedis app -- HelloAgain:

 

package org.jredis.examples;

import org.jredis.ClientRuntimeException;
import org.jredis.Command;
import org.jredis.JRedis;
import org.jredis.RedisException;
import org.jredis.connector.ProviderException;
import org.jredis.ri.alphazero.JRedisClient;
import org.jredis.ri.alphazero.support.Encode;

/**
 * Note this program will set a (hopefully non-coliding!) key in your DB 13.
 * 
 * @author Joubin Houshyar
 *
 */
public class HelloAgain {
        public static final String key = "jredis::examples::HelloAgain::message";
        public static void main(String[] args) {
                String password = "";
                if(args.length > 0) password  = args[0];
                new HelloAgain().run(password);
        }

        private void run(String password) {
                try {
                        JRedis  jredis = new JRedisClient();

                        if(!password.equals("")) 
                                jredis.auth(password);
                        
                        jredis.ping().select(13);
                        
                        if(!jredis.exists(key)) {
                                jredis.set(key, "Hello Again!");
                                System.out.format("Hello!  You should run me again!\n");
                                return;
                        }
                        
                        String msg = Encode.toStr ( jredis.get(key) );
                        
                        System.out.format("%s\n", msg);
                }
                catch (RedisException error){
                        if (error.getCommand()==Command.PING){
                                System.out.format("I'll need that password!  Try again with password as command line arg for this program.\n");
                        }
                }
                catch (ProviderException bug){
                        System.out.format("Oh no, an 'un-documented feature':  %s\nKindly report it.", bug.getMessage());
                }
                catch (ClientRuntimeException problem){
                        System.out.format("%s\n", problem.getMessage());
                }
        }
}

 The essentials:

 

1) Get a connection implementing JRedis interface. (You'll want to code to this interface to minimize the impact of changes behind the scene) like this:

 

  JRedis      jredis = new JRedisClient();

 2) Do you have a requirepass jredis in your 'redis.conf' ? Then do this:

 

  jredis.auth(password);
 3) Use the JRedis api, which is an analog of the Redis command set.

Want to bind a value to a key (map semantics)?

Use the Redis 'String' commands:

 

jredis.set(myKey, myValue); 

 What can 'myKey' be? Any java.lang.String value that does not contain \r, \n, and space. Other than that, Redis and JRedis support UTF-8 keys:

 

 String asciiKey = "ascii-key";
                        String utf8key_Russian = "фывапро";
                        String utf8key_Chinese = "漢字[汉字]";
                                                                                                String utf8key_Persian = "مهندس";
                        String variousKeys[] = {asciiKey, utf8key_Russian, utf8key_Chinese, utf8key_Persian};
                        String value = "some data";
                        
                        for(String key : variousKeys){
                                System.out.format("using %s as key for SET ...", key);
                                redis.set(key, value);
                                System.out.format("...and we get:\n\t  %s => '%s'\n", key, value);
                        }
 What can 'myValue' be?

JRedis is to the metal. So you can pass byte[]. In fact, if you are after high performance, you'll want to avoid passing java.lang.String, unless that is precisely what you want stored. Redis itself will accept anything for the value. You can pass up to 1MB of \r\n or zeros, if you feel like it. Its a blob.

So, do you have a Java (Serializable) object you want to add as a member of a set?

Here is how:

 

 // lets make a 100 SimpleBean instances and add them to our
                        // 'object_set' key (which is a Redis SET)

                        int objcnt = 100;
                        System.out.format ("Creating and saving %d Java objects to redis ...", objcnt);

                        for(int i=1; i<objcnt; i++){
                                // instance it
                                SimpleBean      obj = new SimpleBean ("bean #" + i);

                                // get the next available object id from our Redis counter using INCR command
                                int id = redis.incr("SimpleBean::next_id")

                                // we can bind it a unique key using map (Redis "String") semantics now
                                String key = "objects::SimpleBean::" + id;

                                // voila: java object db
                                redis.set(key, obj);
                                
                                // and lets add it to this set too since this is so much fun
                                redis.sadd("object_set", obj);
                        }

                        System.out.format (" and done.\n");
 And how do I get my values back to proper types?

So, to repeat, Redis treats values as blobs, just byte[]s, so JRedis api reflects that and returns either byte[] or List<byte[]> (for set and list ops).

So, to help out there is (as of r16 but this will be improved so remember this bit is in flux), Encode.

Here's how we get our objects back using Encode.decode(byte[] bytes), which which have imported using import static to make things easier:

 

 // lets get all those objects in that object set
                        // (Remember: JRedis is NOT maintaining a type system for you, so
                        // if you have other kinds of blobs in that set, the object stream is not going to like it

                        List<SimpleBean>  members = decode (redis.smembers("object_set"));

                        for(SimpleBean obj : members) {
                                System.out.format("a member of 'object-set' => %s\n", obj.toString());
                        }
 And there you are.

byte[]s go in, and byte[]s come out. If you want to convert to Number and String, check out the methods in DefaultCodec class.

And its as simple as that.

/Enjoy!

转:http://code.google.com/p/jredis/wiki/JRedisQuickStart

分享到:
评论

相关推荐

    基于matlab实现的空间调制通信过程,包含信号调制、天线选择等发送过程,以及采用最大似然估计的检测过程 .rar

    基于matlab实现的空间调制通信过程,包含信号调制、天线选择等发送过程,以及采用最大似然估计的检测过程。.rar

    基于matlab的关于生猪养殖场经营管理的研究.docx

    本文档是课题研究的研究报告内含调研以及源码设计以及结果分析

    网络作为特征提取器-python源码.zip

    网络作为特征提取器-python源码.zip

    JavaScript-javaweb项目

    JavaScript-javaweb项目

    node-v12.11.0-linux-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    仿Slideby触屏版html5响应式手机wap网站模板下载.zip

    触屏版自适应手机wap软件网站模板 触屏版自适应手机wap软件网站模板

    node-v10.18.1-linux-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    IEC 60695-11-3:2012.pdf

    IEC 60695-11-3:2012.pdf

    2021-2010上市公司和讯网社会责任评级CSR-股东员工客户消费者环境社会责任分项评级

    上市公司和讯网社会责任评级CSR-股东责任员工责任客户消费者环境社会责任分项评级 得分(2010-2021年) "中国上市公司-和讯网社会责任数据"是 一份来自和讯网的数据集,它同步并收集了中国上市公司关于社会责任的相关信息。包括了 公司在股东责任、员工责任、供应商客户消费者权益责任、环境责任、社会责任中的表现和 成绩,以反映公司承担社会责任的程度。可以帮助大家了解公司在承担社会责任方面的具体 表现。这对于研究公司社会责任与公司业绩、公司声誉、公司风险等方面的关系具有参考意 义。 一、数据介绍 数据名称:上市公司和讯网社会责任评级CSR-股东责任员工责任 客户消费者环境社会责任分项评级得分 数据年份:2010-2021年 样本数量:每 年含2300-4600左右上市公司数据,总数据量40058条(注:因披露口径原因 ,2021年仅有430+上市公司数据) 数据格式:Excel面板数据 二、指标说 明 共计11个指标:股票名称、股票代码、年份、总得分、等级、股东责任、员工责任、 供应商客户和消费者权益责任、环境责任、社会责任、统计日期 三、部分excel数据 展示

    使用opencv进行人脸识别和对比-python源码.zip

    使用opencv进行人脸识别和对比-python源码.zip

    EmotionVGGnet情绪识别-python源码.zip

    EmotionVGGnet情绪识别-python源码.zip

    node-v12.1.0-linux-arm64.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    MediaPipe人体姿势估计-python源码.zip

    MediaPipe人体姿势估计-python源码.zip

    构造并使用决策树进行分类-python源码.zip

    构造并使用决策树进行分类-python源码.zip

    <2024年5月软考高项极限冲刺>《1 考试简介》

    <2024年5月软考高项极限冲刺>《1 考试简介》

    【特效超多】仿德国开元旅游触屏版html5手机wap旅游网站模板下载.zip

    【特效超多】仿德国开元旅游触屏版html5手机wap旅游网站模板下载.zip

    JSP基于WEB网上论坛设计与实现(源代码+论文+开题报告+答辩PPT+外文翻译).zip

    JSP基于WEB网上论坛设计与实现(源代码+论文+开题报告+答辩PPT+外文翻译)

    仿YOKA服饰美容3G手机wap女性网站模板.zip

    触屏版自适应手机wap软件网站模板 触屏版自适应手机wap软件网站模板

    node-v12.0.0-linux-ppc64le.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

    node-v4.4.0.tar.xz

    Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。

Global site tag (gtag.js) - Google Analytics