The Lua script will find all the keys that match the string in ARGV (lua is “1” based, not zero based). That should get me all the soldiers with X,Y coordinates in the 0-99 by 0-99 block. Var scriptReturnValueArray = (RedisResult)rowDataAsRedisResult Īs you can see, I put in a key search of SX100Y100_*. Var rowDataAsRedisResult = (RedisResult)rowDataFromRedis Var rowDataFromRedis = (GetListOfSoldiers, null, redisValueArgs) Var uniqueNumber = i.ToString().PadLeft(5, '0') The method I designed to perform this task looks like this: private static void GenerateData() Then I tried it with 5000, and finally 50,000. I started with only 500 records and tested my code. My next task was to seed the Redis database with a lot of data. Later I upped it to a range that spans 0 to 99. So I adjusted my algorithm to assume a zero starting number and used 0 through 49. Otherwise, I could adjust it to 1 to 50, but in the programming world, that would get ugly fast. As I mentioned in my last article the X50 by Y50 represented coordinates between 0 to 50. The first “problem” I ran into was that I was off-by-one on my block numbers. There is a block size parameter to change this, but be aware that it will break the unit tests because they are written to test edge cases for 100 by 100 blocks. In my original discussion I used a 50 by 50 block, but after some testing I decided that a 100 by 100 block made more sense. I created some unit tests to make sure the BlockAddress getter was computing the correct block addresses. Var y = (Y / _blockSize + 1) * _blockSize Var x = (X / _blockSize + 1) * _blockSize I only encoded the X,Y coordinates and a property to convert the X,Y coordinates into the first part of a Redis key that will be used to store the data: public class Soldier Next I created a class to represent the data that will store the soldier information. The interface for this class looks like this: public interface IRedisConnectionManager () => ConnectionMultiplexer.Connect(ConfigOptions.Value)) Private readonly Lazy _connectionMux = new Lazy( Var configOptions = new ConfigurationOptions() Ĭ(_redisServerAddress) ĬonfigOptions.ClientName = "RedisConnection" ĬonfigOptions.AbortOnConnectFail = false Private static readonly Lazy ConfigOptions = new Lazy(() => _redisServerAddress = redisServerAddress Public RedisConnectionManager(string redisServerAddress) Private static string _redisServerAddress So here’s the code that I use in such a situation: public class RedisConnectionManager : IRedisConnectionManager For a production system, you’ll need a connection pool. I’ll warn you that you cannot use the connection manager code as is in a production system. You can create a throw-away project using the code that the StackExchange Redis site shows (see StackExchange Redis Usage Wiki). I use the StackExchange.Redis NuGet package to connect to a Redis server from C#. The first thing I created was a Redis connection manager. You’ll have to put some data into your Redis server first, but that should be enough Redis knowledge to get you warmed up. That will retrieve all keys that start with SX50Y50_. If you have a bunch of keys that are named the way I named them in the previous article (SX50Y50_00001, etc.), then you can do something like this: keys SX50Y50_* I usually only use the star in combination with a substring. The keys command can work with wild cards. To see all keys you can use the previous keys command: keys * First, to clear all keys in all databases of Redis you can use the flush command: flushall You need two basic commands to get started. Next, you’ll need to open the Windows Services and set it to run:.Then use the following command to run Redis as a service (there are two minus signs in front of the service install instruction): redis-server –service-install.Open a command window (in Admin mode) and navigate to where you unzipped your Redis files (go to the bin/windows directory).Unzip the files into a Redis directory on your hard drive.This version includes Sentinel, which is a clustering setup. First, go to this GitHub URL and download the zip file of Redis for Windows: Redis Sentinel. Just in case you’re not as much of a Redis Guru as I am. So I’m going to start with how to install the windows Redis and a couple of basic commands. I have Redis installed on my desktop (and my work laptop) because I work with Redis on many projects. In this post, I’m going to drill down and show how to do it. If you read part 2, you’ll remember my discussion on using Redis to store the active map soldiers. In those articles I used an MMORPG game as an example program/system to scale horizontally. This is a continuation of the Scaling Horizontally blog posts that I started earlier (see part 1 and part 2).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |