{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Day 2 Course Notes" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from rediscluster import StrictRedisCluster\n", "startup_nodes = [{\"host\": \"127.0.0.1\", \"port\": \"7001\"}]\n", "messenger_cluster = StrictRedisCluster(startup_nodes=startup_nodes, decode_responses=True)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'127.0.0.1:7001': 0,\n", " '127.0.0.1:7002': 0,\n", " '127.0.0.1:7003': 0,\n", " '127.0.0.1:7004': 0,\n", " '127.0.0.1:7005': 0,\n", " '127.0.0.1:7006': 0}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "messenger_cluster.dbsize()" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "person_consumer = messenger_cluster.pubsub()\n", "person_consumer.subscribe(\"chat_room\")\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'channel': 'chat_room',\n", " 'data': 'with great message\\n',\n", " 'pattern': None,\n", " 'type': 'message'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "person_consumer.get_message()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": true }, "outputs": [], "source": [ "person_consumer.subscribe(\"chat_room\", \"chat_breakroom\")" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import redis\n", "cloud_redis = redis.StrictRedis(host='159.203.239.23')\n", "cloud_redis.dbsize()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "slave_redis = redis.StrictRedis(host='159.203.239.23', port=6380)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "slave_redis.dbsize()" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "slave_redis.slaveof(host='159.203.239.23', port=6379)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cloud_redis.set(\"master-key\", \"This is a master key\")" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "slave_redis.keys(\"*\")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[b'master-key']" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cloud_redis.keys(\"*\")" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d006f1a90249474274c76f5be725b8f5804a346b\n" ] }, { "data": { "text/plain": [ "[b'Happy', b'Lucky']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import redis\n", "vm_redis = redis.StrictRedis()\n", "lua_script = \"return {KEYS[1], ARGV[1]}\"\n", "saved_sha1 = vm_redis.script_load(lua_script)\n", "print(saved_sha1)\n", "vm_redis.evalsha(saved_sha1, 1, \"Happy\", \"Lucky\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "EVALSHA a42" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.0" } }, "nbformat": 4, "nbformat_minor": 0 }