Redis install-connect server demo (1 section)

来源:互联网 发布:js时间转换成时间戳 编辑:程序博客网 时间:2024/06/14 17:00

Redis 开源免费,是一个高性能的key-value数据库,通常被称为数据结构服务器
key - value 缓存产品有以下三个特点:

Redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用。Redis不仅仅支持简单的key-value类型的数据,同时还提供list,set,zset,hash等数据结构的存储。Redis支持数据的备份,即master-slave模式的数据备份。

install

http://www.runoob.com/redis/redis-install.html(注:Window ,Linux ,Ubuntu 下安装,内含Redis各版本下载)

demo 1:

打开一个 cmd 窗口 使用cd命令切换目录到 redis 目录,运行 redis-server.exe redis.windows.conf 。输入之后回车,会显示如下界面:


另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379 。设置键值对 set myKey democreen取出键值对 get myKey
如图:

注释:127.0.0.1:6379  是Redis服务端。  


下面写一个例子

运行效果如图:




主要文件:pom.xml,RedisExample.java

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>DemocreenProject</groupId>    <artifactId>RedisTest</artifactId>    <version>1.0-SNAPSHOT</version>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>    </properties>    <dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>3.8.1</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.redisson</groupId>            <artifactId>redisson</artifactId>            <version>1.0.2</version>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.7.7</version>        </dependency>    </dependencies></project>

RedisExample.java

/** * Copyright:厦门至恒天地信息技术有限公司 版权所有 违者必究 2017 */package com.demo.redis;import java.util.Queue;import java.util.Set;import java.util.concurrent.ConcurrentMap;import org.redisson.Config;import org.redisson.Redisson;public class RedisExample {    public static void main(String[] args) {        // 1.初始化        Config config = new Config();        config.setConnectionPoolSize(10);        config.addAddress("127.0.0.1:6379");        Redisson redisson = Redisson.create(config);        System.out.println("reids连接成功...");        // 2.测试concurrentMap,put方法的时候就会同步到redis中        ConcurrentMap<String, Object> map = redisson.getMap("FirstMap");        map.put("wuguowei", "男");        map.put("zhangsan", "nan");        map.put("lisi", "女");        ConcurrentMap resultMap = redisson.getMap("FirstMap");        System.out.println("resultMap==" + resultMap.keySet());        // 2.测试Set集合        Set mySet = redisson.getSet("MySet");        mySet.add("wuguowei");        mySet.add("lisi");        Set resultSet = redisson.getSet("MySet");        System.out.println("resultSet===" + resultSet.size());        //3.测试Queue队列        Queue myQueue = redisson.getQueue("FirstQueue");        myQueue.add("wuguowei");        myQueue.add("lili");        myQueue.add("zhangsan");        myQueue.peek();        myQueue.poll();        Queue resultQueue1=redisson.getQueue("FirstQueue");        System.out.println("resultQueue==="+resultQueue1);        // 关闭连接        redisson.shutdown();    }}


先启动Redis服务端 (使用cd命令切换目录到 redis目录, 运行 redis-server.exe redis.windows.conf)

运行结果如下:



1node-小结:

熟悉了解redis.conf配置文件,redis配置,数据类型,以及redis命令。

参考网址:

http://www.runoob.com/redis/redis-conf.html

java使用 Redis 示例2( Java redis 驱动

http://www.runoob.com/redis/redis-java.html



原创粉丝点击