zoukankan      html  css  js  c++  java
  • redis原理及使用

    redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hashs(哈希类型)

    这些数据类型都支持push/pop、add/remove及取交集并集和差集及更丰富的操作,而且这些操作都是原子性的.
    在此基础上,redis支持各种不同方式的排序.与memcached一样,为了保证效率,数据都是缓存在内存中.
    区别的是redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件,并且在此基础上实现了master-slave(主从)同步.
     
    Redis的优点:
     
    性能极高 – Redis能支持超过 100K+ 每秒的读写频率。
    丰富的数据类型 – Redis支持二进制案例的 Strings, Lists, Hashes, Sets 及 Ordered Sets 数据类型操作。
    原子 – Redis的所有操作都是原子性的,同时Redis还支持对几个操作全并后的原子性执行。
    丰富的特性 – Redis还支持 publish/subscribe, 通知, key 过期等等特性。
     
    下面是官方的bench-mark数据:
    测试完成了50个并发执行100000个请求。
    设置和获取的值是一个256字节字符串。
    Linux box是运行Linux 2.6,这是X3320 Xeon 2.5 ghz。
    文本执行使用loopback接口(127.0.0.1)。
    结果:写的速度是110000次/s,读的速度是81000次/s 。
     
    ===================redis使用说明:
    1.cmd中启动
    D:Redis>redis-server.exe redis.conf
    2.执行命令:
    D:Redis>redis-cli.exe -h 172.16.147.121 -p 6379
    3.设置数值:
    redis 172.16.147.121:6379> set city Shanghai
    4.获取数值:
    redis 172.16.147.121:6379> get city
    ====================
    using ServiceStack.Redis;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace redis
    {
        public partial class Form1 : Form
        {
            RedisClient redisClient = null;
            public Form1()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                redisClient = new RedisClient("localhost"6379);//redis服务IP和端口
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                redisClient.Add<string>("name""郭泽峰");
            }
     
            private void button3_Click(object sender, EventArgs e)
            {
                MessageBox.Show(redisClient.Get<string>("name"));
            }
        }
    }
     
    ==========jedis:
      //连接redis服务器,192.168.0.100:6379
     19         jedis = new Jedis("192.168.0.100"6379);
     20         //权限认证
     21         jedis.auth("admin");  
      jedis.set("name","xinxin");//向key-->name中放入了value-->xinxin  
     31         System.out.println(jedis.get("name"));//执行结果:xinxin  
    ===主从监视器:sentinels
     
      JedisPoolConfig poolConfig = new JedisPoolConfig();
    12         String masterName = "mymaster";
    13         Set<String> sentinels = new HashSet<String>();
    14         sentinels.add("192.168.1.97:26379");
    15         sentinels.add("192.168.1.96:26379");
    16         JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, poolConfig);
    17         HostAndPort currentHostMaster = jedisSentinelPool.getCurrentHostMaster();
    18         System.out.println(currentHostMaster.getHost()+"--"+currentHostMaster.getPort());//获取主节点的信息
    19         Jedis resource = jedisSentinelPool.getResource();
    20         String value = resource.get("a");
    21         System.out.println(value);//获得键a对应的value值
    22         resource.close();
    23     }
     
    ================
    JedisPoolConfig poolConfig = new JedisPoolConfig();
    12         Set<HostAndPort> nodes = new HashSet<HostAndPort>();
    13         HostAndPort hostAndPort = new HostAndPort("192.168.1.99"7000);
    14         HostAndPort hostAndPort1 = new HostAndPort("192.168.1.99"7001);
    15         HostAndPort hostAndPort2 = new HostAndPort("192.168.1.99"7002);
    16         HostAndPort hostAndPort3 = new HostAndPort("192.168.1.99"7003);
    17         HostAndPort hostAndPort4 = new HostAndPort("192.168.1.99"7004);
    18         HostAndPort hostAndPort5 = new HostAndPort("192.168.1.99"7005);
    19         nodes.add(hostAndPort);
    20         nodes.add(hostAndPort1);
    21         nodes.add(hostAndPort2);
    22         nodes.add(hostAndPort3);
    23         nodes.add(hostAndPort4);
    24         nodes.add(hostAndPort5);
    25         JedisCluster jedisCluster = new JedisCluster(nodes, poolConfig);//JedisCluster中默认分装好了连接池.
    26         //redis内部会创建连接池,从连接池中获取连接使用,然后再把连接返回给连接池
    27         String string = jedisCluster.get("a");
    28         System.out.println(string);            
    =======================================
  • 相关阅读:
    一周以来工作总结关于位图索引
    再学学表的分区
    PostgreSQL学习笔记
    通过vc助手设置快捷注释
    c语言中unsigned类型和普通类型间的转换
    LVS环境搭建入门
    java学习路线
    linux下删除当前文件夹中按时间排序的前N个文件夹
    RHEL下安装jdk和tomcat
    TDD 强迫你 Program to Interface
  • 原文地址:https://www.cnblogs.com/piwefei/p/10402651.html
Copyright © 2011-2022 走看看