const redis = require('redis')
require('dotenv').config();
const client = redis.createClient({
url: `redis://${process.env.REDIS_HOST}:${process.env.REDIS_PORT}`,
// EX) 'redis://localhost:6379'
})
const geo = require('georedis').initialize(client)
// String
client.set('name', 'MyName')
client.get('name', (error, reply) => {
console.log(reply);
})
// Object
client.hmset('friends', 'name', 'zero', 'age', 24);
client.hgetall('friends', (error, obj) => {
console.log(obj);
})
// List
client.rpush('fruits', 'apple', 'orange', 'apple') // Array.push
client.lpush('fruits', 'banana', 'pear'); // Array.unshift
client.lrange('fruits', 0, -1, (error, arr) => { // Start, End Index
console.log(arr);
})
// Set
client.sadd('animals', 'dog', 'cat', 'bear', 'cat', 'lion');
client.smembers('animals', (error, set) => {
console.log(set);
})
// Delete
client.del('fruits')
// is Exists
const isExists = client.exists('name');
console.log('Hello isExists :', isExists);
// 6379> lrange fruits 0 -1
client.lrange('fruits', 0, -1, (error, arr) => {
console.log(arr);
})
// DOC : https://www.npmjs.com/package/georedis
// 단일 등록
// 6379> GEOADD geo:locations -79.4167 43.6667 'Toronto'
geo.addLocation(
'Toronto', {latitude: 43.6667, longitude: -79.4167},
(error, reply) => {
if (error) console.error(error);
else console.log('added location: ', reply);
})
const locationSet = {
'Toronto': {latitude: 43.6667, longitude: -79.4167},
'Philadelphia': {latitude: 39.9523, longitude: -75.1638},
'Palo Alto': {latitude: 37.4688, longitude: -122.1411},
'San Francisco': {latitude: 37.7691, longitude: -122.4449},
'St. John\'s': {latitude: 47.5500, longitude: -52.6667},
'New York': {latitude: 40.7143, longitude: -74.0060},
'Twillingate': {latitude: 49.6500, longitude: -54.7500},
'Ottawa': {latitude: 45.4167, longitude: -75.7000},
'Calgary': {latitude: 51.0833, longitude: -114.0833},
'Mumbai': {latitude: 18.9750, longitude: 72.8258}
}
// Object 형식으로 등록
// GEOADD
// 6379> GEOADD geo:locations -79.4167 43.6667 'Toronto' -75.1638 39.9523 'Philadelphia' ...
geo.addLocations(locationSet, function (err, reply) {
if (err) console.error(err)
else console.log('added locations:', reply)
})
// GEOPOS
// 6379> GEOPOS geo:locations Toronto
geo.location('Toronto', function (err, location) {
if (err) console.error(err)
else console.log('Location for Toronto is: ', location.latitude, location.longitude)
})
// GEOPOS multiple locations
// 6379> GEOPOS geo:locations 'Toronto' 'Philadelphia' 'Palo Alto' 'San Francisco' 'Ottawa'
geo.locations(['Toronto', 'Philadelphia', 'Palo Alto', 'San Francisco', 'Ottawa'], function (err, locations) {
if (err) console.error(err)
else {
for (var locationName in locations) {
console.log(locationName + "'s location is:", locations[locationName].latitude, locations[locationName].longitude)
}
}
})
// GEORADIUS
// 6379> GEORADIUS geo:locations -79.403723 43.646838 5000 m
geo.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, function (err, locations) {
if (err) console.error(err)
else console.log('nearby locations:', locations)
})
var options = {
withCoordinates: true, // 일치하는 항목의 경도, 위도 좌표를 반환, default false
withHashes: true, // Geohash 인코딩으로 반환, default false
withDistances: true, // 지정된 중심에서 항목의 거리를 반환, default false
order: 'ASC', // 가장 가까운 것 부터 정렬, 'DESC' or true (same as 'ASC'), default false
units: 'm', // m (Meters), km (Kilometers), mi (Miles), ft (Feet), default 'm'
count: 100, // N개의 일치하는 항목으로 결과를 제한, default undefined (All)
accurate: true // Useful if in emulated mode and accuracy is important, default false
}
// look for all points within ~5000m of Toronto with the options.
// GEORADIUS
// GEORADIUS geo:locations -79.403723 43.646838 5000 m WITHCOORD WITHDIST WITHHASH COUNT 100 ASC
/*
1) 1) "Toronto"
2) "2443.4497"
3) (integer) 1973332721058153
4) 1) "-79.4166979193687439"
2) "43.66669965801614239" */
geo.nearby({latitude: 43.646838, longitude: -79.403723}, 5000, options, function (err, locations) {
if (err) console.error(err)
else console.log('nearby locations:', locations)
})
// GEORADIUSBYMEMBER
// GEORADIUSBYMEMBER geo:locations 'Toronto' 5000 m WITHCOORD WITHDIST WITHHASH COUNT 100 ASC
/*
1) 1) "Toronto"
2) "0.0000"
3) (integer) 1973332721058153
4) 1) "-79.4166979193687439"
2) "43.66669965801614239" */
geo.nearby('Toronto', 5000, options, function (err, locations) {
if (err) console.error(err)
else console.log('nearby locations:', locations)
})
// ZREM
// ZREM geo:locations 'New York'
geo.removeLocation('New York', function (err, reply) {
if (err) console.error(err)
else console.log('removed location:', reply)
})
// OR Quicker for Bulk Removals
// ZREM multiple locations
// ZREM geo:locations 'New York' 'St. John\'s' 'San Francisco'
geo.removeLocations(['New York', 'St. John\'s', 'San Francisco'], function (err, reply) {
if (err) console.error(err)
else console.log('removed locations', reply)
})