{"id":18577471,"url":"https://github.com/pjt3591oo/redis-sample","last_synced_at":"2026-04-13T01:09:18.820Z","repository":{"id":100332902,"uuid":"397161952","full_name":"pjt3591oo/redis-sample","owner":"pjt3591oo","description":null,"archived":false,"fork":false,"pushed_at":"2021-08-20T07:55:44.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-17T15:26:27.391Z","etag":null,"topics":["nodejs","redis","socket-io"],"latest_commit_sha":null,"homepage":"https://blog.naver.com/pjt3591oo/222477954475","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pjt3591oo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-17T08:00:02.000Z","updated_at":"2021-08-20T07:57:44.000Z","dependencies_parsed_at":"2023-05-13T22:15:30.558Z","dependency_job_id":null,"html_url":"https://github.com/pjt3591oo/redis-sample","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjt3591oo%2Fredis-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjt3591oo%2Fredis-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjt3591oo%2Fredis-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pjt3591oo%2Fredis-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pjt3591oo","download_url":"https://codeload.github.com/pjt3591oo/redis-sample/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254453434,"owners_count":22073608,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","redis","socket-io"],"created_at":"2024-11-06T23:29:16.230Z","updated_at":"2026-04-13T01:09:13.781Z","avatar_url":"https://github.com/pjt3591oo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"* redis 실행\n\n```sh\n$ docker run -i -t --name myredis -d -p 6379:6379 redis\n```\n\n* 컨테이너 접속\n\n```sh\n$ docker exec -it myredis /bin/bash\n\nroot@d61d7f5c8271:/data# redis-cli\n\n127.0.0.1:6379\u003e \n```\n\n* GUI 툴: p3x-redis-ui\n\n```\nhttps://www.electronjs.org/apps/p3x-redis-ui\n```\n\n* connect\n\n```js\nconst redis = require(\"redis\");\nconst client = redis.createClient({\n  host: 'localhost',\n  port: 6379\n});\n```\n\n# 자료구조\n\n* string\n\n```js\n\nclient.set(\"key\", \"mung\", (err, result) =\u003e {\n  console.log('set result');\n  console.log(result);\n});\n\nclient.get(\"key\", (err, result) =\u003e {\n  console.log('get result');\n  console.log(result);\n});\n```\n\n* hash\n\n```js\n// 키: friends\n// 값: {name: 'mung', 'age': 29}\nclient.hmset('friends', 'name', 'mung', 'age', 29);\nclient.hgetall('friends', (err, obj) =\u003e {\n  console.log(obj); // { name: 'mung', age: '29' }\n});\n```\n\n* list\n\n```js\n// 오른쪽에서 하나씩 밀어넣기\n// 키: fruits\n// 값: 'apple1', 'orange2', 'apple3' 순서대로 오른쪽에서 push\nclient.rpush('fruits', 'apple1', 'orange2', 'apple3');\n\n// 왼쪽에서 하나씩 밀어넣기\n// 값: 'banana', 'pear' 순서대로 왼쪽에서 push\nclient.lpush('fruits', 'banana1', 'pear2');\n\n// 키, 시작, 끝(-1은 마지막을 의미)\nclient.lrange('fruits', 0, -1, (err, arr) =\u003e {\n  console.log(arr); // ['pear2', 'banana1', 'apple1', 'orange2', 'apple3']\n});\n```\n\n* set\n\n순서를 보장하지 않으며 중복을 허용하지 않음\n\n```js\nclient.sadd('animals', 'dog', 'cat', 'bear', 'cat', 'lion');\nclient.smembers('animals', (err, set) =\u003e {\n  console.log(set); // ['cat', 'dog', 'bear', 'lion']\n});\n```\n\n* sorted set\n\n정렬된 set \n\n첫 번째 인자는 key\n두 번쨰 인자는 다음과 같다.\n(180, 'zero'), (158, 'aero'), (167, 'nero'), (166, 'hero')\n\n```js\nclient.zadd('height', 180, 'mung0', 168, 'mung1', 176, 'mung2', 172, 'mung3');\nclient.zrange('height', 0, -1, (err, sset) =\u003e {\n  console.log(sset); // ['mung0', 'mung2', 'mung3', 'mung1'\n});\n```\n\n* geo\n\n위/경도\n\nlongitude가 먼저등장\n\n첫 번째 인자: key\n두 번쨰 인자부터는 다음과 같다\n\n(126.97, 37.56, 'seoul'), (129.07, 35.17, 'busan'), (126.70, 37.45, 'incheon')\n\n```js\nclient.geoadd('cities', 126.97, 37.56, 'seoul', 129.07, 35.17, 'busan', 126.70, 37.45, 'incheon');\n\n// 두 위치간 거리\nclient.geodist('cities', 'seoul', 'busan', (err, dist) =\u003e {\n  console.log(dist); // 325619.5465\n});\n\n// 특정위치에서 해당하는 지역\nclient.georadius('cities', 126.8, 37.5, 50, 'km', (err, cities) =\u003e {\n  console.log(cities); // ['incheon', 'seoul']\n});\n```\n\n# utils\n\n* 키 지우기\n\n```js\nclient.del('key');\n```\n\n* 키 존재 확인\n\n```js\nclient.exists('height'); // 1: 있을 때, 0: 없을 때\n```\n\n* 키 이름 변경\n\n```js\nclient.rename('cities', 'countries');\n```\n\n찾는 키가 없으면 에러발생\n\n```\nReplyError: ERR no such key\n    at parseError (/Users/fles_dev01/Desktop/redis-rrr/node_modules/redis-parser/lib/parser.js:179:12)\n    at parseType (/Users/fles_dev01/Desktop/redis-rrr/node_modules/redis-parser/lib/parser.js:302:14) {\n  command: 'RENAME',\n  args: [ 'animals', 'pets' ],\n  code: 'ERR'\n}\n```\n\n# pub/sub\n\npub: 생산자\nsub: 구독자\n\n* sub\n\n```js\nconst redis = require(\"redis\");\n\nconst subscriber = redis.createClient();\n\n\nlet messageCount = 0;\n\nsubscriber.on(\"subscribe\", function(channel, count) {\n  console.log(channel, '구독시작');\n});\n\nsubscriber.on(\"message\", function(channel, message) {\n  messageCount += 1;\n  console.log(\"Subscriber received message in channel '\" + channel + \"': \" + message);\n\n  if (messageCount \u003e 5) {\n    subscriber.unsubscribe();\n    subscriber.quit();\n  }\n});\n\nsubscriber.subscribe(\"a channel\");\n```\n\n* pub\n\n```js\nconst redis = require(\"redis\");\n\nconst publisher = redis.createClient();\n\npublisher.publish(\"a channel\", \"a message\");\npublisher.publish(\"a channel\", \"another message\");\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpjt3591oo%2Fredis-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpjt3591oo%2Fredis-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpjt3591oo%2Fredis-sample/lists"}