redis 学习手册之java和php连接redis操作

1、Java开发

1.1、安装redis驱动

要在Java程序中使用使用操作Redis,需要确保有Redis的Java驱动程序

jedis.jar,确保下载最新版本

 

下载地址:http://repo1.maven.org/maven2/redis/clients/jedis
/2.8.0/jedis-2.8.0.jar 

 

将jedis-2.8.0.jara 拷贝到java项目的类目录下

 

1.2、 redis实例开发

新建java工程“ausjedis”

加载jedis-2.8.0.jar

创建ausjedis类

1、连接redis库

package aus.jedis.demo;

importredis.clients.jedis.Jedis;

public class ausjedis {

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       Jedis jedis = new Jedis("192.168.188.26", 6379);

       jedis.auth("daphne");

       if (jedis.ping().equals("PONG"))

       {

           System.out.println("Connection to server sucessfully");

       }

       jedis.set("name", "zhenyun.su from java");

       String sName = jedis.get("name");

       System.out.println(sName);

    }

}

 

运行结果:

Connection to server sucessfully

zhenyun.su from java

 

2、操作list数据类型

package aus.jedis.demo;

import java.util.List;

importredis.clients.jedis.Jedis;

public class ausjedis {

    public static void main(String[] args) {

       // TODO Auto-generated method stub

       Jedis jedis = new Jedis("192.168.188.26", 6379);

       jedis.auth("daphne");

       if (jedis.ping().equals("PONG"))

       {

           System.out.println("Connection to server sucessfully");

       }     

       jedis.lpush("auslist1", "one", "two");

       jedis.lpush("auslist1", "three", "four");

      

       Listlist = jedis.lrange("auslist1", 0, jedis.llen("auslist1"));

       for (int i=0; i<list.size(); i++){

           System.out.println(list.get(i));

       }     

    }

}

运行结果:

   Connection toserver sucessfully

zhenyun.su from java

four

three

two

one

four

three

two

one

 

2、Php开发

1.1、安装

在PHP程序中使用Redis,需要确保我们有Redis的PHP驱动程序。

从github上资料库https://github.com/nicolasff/phpredis下载phpredis。

下载了它以后,将文件上传到src目录

# cd /usr/local/src

# tar zxvfphpredis-2.2.7.tar.gz

# cd phpredis-2.2.7

 

#/usr/local/php/bin/phpize

# ./configure

 

报错:configure: error: Cannotfind php-config. Please use --with-php-config=PATH

# find / -name  php-config

# ./configure--with-php-config=/usr/local/php/bin/php-config

执行成功

执行编译安装

# make && makeinstall

--------------------------------

Build complete.

Don't forget to run 'maketest'.

 

Installing sharedextensions:    /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/

 

说明安装成功

 

 

配置php.ini /usr/local/php/etc/php.ini

extension = redis.so

 

重新apache服务,就可以使用

 

 

1.2、 redis实例开发

新建php“redis_demo.php”文件

# Cd /usr/local/apache/htdocs

 

   //Connecting to Redis server onlocalhost

   $redis = new Redis();

   $redis->connect('127.0.0.1',6379);

   $redis->auth('daphne');

   echo "Connection to serversucessfully";

   //check whether server is runningor not

   echo "Server is running:"+ $redis->ping();

?>

 

将apache httpd.conf 配置为 documentroot 为/usr/local/apache/htdocs

 

通过浏览器执行:

 redis 学习手册之java和php连接redis操作_第1张图片

2、操作list数据类型

 

   //Connecting to Redis server onlocalhost

   $redis = new Redis();

   $redis->connect('127.0.0.1',6379);

   $redis->auth('daphne');

   echo "Connection to serversucessfully";

   //check whether server is runningor not

   echo "Server is running:"+ $redis->ping();

 

  $redis->lpush("auslistphp", "Redis");

  $redis->lpush("auslistphp", "Mongodb");

  $redis->lpush("auslistphp", "Mysql");

   // Get the stored data and print it

   $arList =$redis->lrange("auslistphp", 0 ,5);

   echo "
Storedstring in redis:
";

   print_r($arList);

?>

redis 学习手册之java和php连接redis操作_第2张图片

您觉的有所收获,请保持持续的关注。
您发现博客中有的纰漏,请指正。
您有更好的建议或更好的实现方式,请赐教。([email protected])

你可能感兴趣的:(redis)