Ejemplo n.º 1
0
 /// <summary>
 /// This method saves a single DataDocument to Redis at a <c>Location</c> within a particular schema. The Redis Key where the document
 /// is saved is determined as <code>schemaId:location</code>. All writes are "last write wins" when multiple writes to the same key are made at the same time.
 ///
 /// Best practice is for all locations used within a "schemaId" to follow the same pattern.
 /// </summary>
 /// <param name="schemaId">The id representing the schema where the document will be stored.</param>
 /// <param name="location">The location within the schema where the document will be stored.</param>
 /// <param name="doc">The <c>DataDocument</c> to persist in Redis.</param>
 /// <param name="expiration">The TTL for the key -> document pair. Defaults to null, indicating that there will not be a TTL.</param>
 /// <returns>A Task to track the asynchronous operation.</returns>
 public async Task Put(string schemaId, Location location, DataDocument doc, TimeSpan?expiration = null)
 {
     string key = schemaId + ":" + location;
     await RetryHelper.RetryOnExceptionAsync(3, async() =>
     {
         var db = RedisConnection.Connection.GetDatabase();
         await db.HashSetAsync(key, doc.ToHashEntries());
         logger.LogDebug("SET {key} {value}.", key, doc);
     });
 }
Ejemplo n.º 2
0
        /// <summary>
        /// This method saves a single DataDocument to Redis at a <c>Location</c> within a particular schema. The Redis Key where the document
        /// is saved is determined as <code>schemaId:location</code>. All writes are "last write wins" when multiple writes to the same key are made at the same time.
        ///
        /// Best practice is for all locations used within a "schemaId" to follow the same pattern.
        /// </summary>
        /// <param name="schemaId">The id representing the schema where the document will be stored.</param>
        /// <param name="location">The location within the schema where the document will be stored.</param>
        /// <param name="doc">The <c>DataDocument</c> to persist in Redis.</param>
        /// <returns>A Task to track the asynchronous operation.</returns>
        public void PutSync(string schemaId, Location location, DataDocument doc)
        {
            string key = schemaId + ":" + location;

            RetryHelper.RetryOnException(3, () =>
            {
                var db = RedisConnection.Connection.GetDatabase();
                db.HashSet(key, doc.ToHashEntries());
                logger.LogDebug("SET {key} {value}.", key, doc);
            });
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This method saves a single DataDocument to Redis at a <c>Location</c> within a particular schema. The Redis Key where the document
        /// is saved is determined as <code>schemaId:location</code>. All writes are "last write wins" when multiple writes to the same key are made at the same time.
        ///
        /// This is a blocking implementation, for non-blocking callers use "Put".
        ///
        /// Best practice is for all locations used within a "schemaId" to follow the same pattern.
        /// </summary>
        /// <param name="schemaId">The id representing the schema where the document will be stored.</param>
        /// <param name="location">The location within the schema where the document will be stored.</param>
        /// <param name="doc">The <c>DataDocument</c> to persist in Redis.</param>
        /// <param name="expiration">The TTL for the key -> document pair. Defaults to null, indicating that there will not be a TTL.</param>
        public void PutSync(string schemaId, Location location, DataDocument doc, TimeSpan?expiration = null)
        {
            string key = schemaId + ":" + location;

            RetryHelper.RetryOnException(3, () =>
            {
                var db = RedisConnection.Connection.GetDatabase();
                db.StringSet(key, doc.ToRedisValue(), expiration);
                logger.LogDebug("SET {key} {value}.", key, doc);
            });
        }
Ejemplo n.º 4
0
        /// <summary>
        /// This method retrieves a subset of a document from Redis containing specific fields.
        /// </summary>
        /// <param name="schemaId">The id representing the schema where the document is stored.</param>
        /// <param name="location">The location within the schema where the document is stored.</param>
        /// <param name="fields">The fields to be retrieved.</param>
        /// <returns>A Task to track the asynchronous retrieval operation.</returns>
        public async Task <DataDocument> Get(string schemaId, Location location, string[] fields)
        {
            var          key      = schemaId + ":" + location.ToString();
            DataDocument response = null;
            await RetryHelper.RetryOnExceptionAsync(3, async() =>
            {
                var db = RedisConnection.Connection.GetDatabase();
                logger.LogDebug("HGET {key} {fields}", key, fields);
                response = new DataDocument(fields.Zip(await db.HashGetAsync(key, Array.ConvertAll(fields, field => (RedisValue)field)), (field, value) => new HashEntry(field, value)).ToArray());
            });

            return(response);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// This method retrieves a document from Redis.
        /// </summary>
        /// <param name="schemaId">The id representing the schema where the document is stored.</param>
        /// <param name="location">The location within the schema where the document is stored.</param>
        /// <returns>A Task to track the asynchronous retrieval operation.</returns>
        public async Task <DataDocument> Get(string schemaId, Location location)
        {
            var          key      = schemaId + ":" + location.ToString();
            DataDocument response = null;
            await RetryHelper.RetryOnExceptionAsync(3, async() =>
            {
                var db = RedisConnection.Connection.GetDatabase();
                logger.LogDebug("HGETALL {key}", key);
                response = new DataDocument(await db.HashGetAllAsync(key));
            });

            return(response);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// This method retrieves a document from Redis.
        /// </summary>
        /// <param name="schemaId">The id representing the schema where the document is stored.</param>
        /// <param name="location">The location within the schema where the document is stored.</param>
        /// <param name="fields">The fields to be retrieved.</param>
        /// <returns>A Task to track the asynchronous retrieval operation.</returns>
        public DataDocument GetSync(string schemaId, Location location, string[] fields)
        {
            var          key      = schemaId + ":" + location.ToString();
            DataDocument response = null;

            RetryHelper.RetryOnException(3, () =>
            {
                var db = RedisConnection.Connection.GetDatabase();
                logger.LogDebug("GET {key}", key);
                response = new DataDocument(fields.Zip(db.HashGet(key, Array.ConvertAll(fields, field => (RedisValue)field)), (field, value) => new HashEntry(field, value)).ToArray());
            });
            return(response);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// This method retrieves a document from Redis.
        /// </summary>
        /// <param name="schemaId">The id representing the schema where the document is stored.</param>
        /// <param name="location">The location within the schema where the document is stored.</param>
        /// <returns>A Task to track the asynchronous retrieval operation.</returns>
        public DataDocument GetSync(string schemaId, Location location)
        {
            var          key      = schemaId + ":" + location.ToString();
            DataDocument response = null;

            RetryHelper.RetryOnException(3, () =>
            {
                var db = RedisConnection.Connection.GetDatabase();
                logger.LogDebug("GET {key}", key);
                response = new DataDocument(db.HashGetAll(key));
            });
            return(response);
        }
Ejemplo n.º 8
0
        static void Main(string [] args)
        {
            RedisPersistenceLayer     redis  = new RedisPersistenceLayer();
            RedisHashPersistenceLayer hashes = new RedisHashPersistenceLayer();
            DataDocument data = new DataDocument();

            data.Set("name", "John Doe");
            data.Set("age", 84);
            data.Add("items", new object[] { 1, 2, 3, "fast", "slow", 90.3, true, false });
            redis.PutSync("person", "1", data);
            hashes.PutSync("person-hash", "1", data);
            Console.WriteLine("data before persistence: " + data);
            Console.WriteLine("data persisted as string: " + redis.GetSync("person", "1"));
            Console.WriteLine("data persisted as hash: " + hashes.GetSync("person-hash", "1"));
            Console.WriteLine("items retrieved from hash field: " + hashes.GetSync("person-hash", "1", "items"));
            Console.WriteLine("name and age retrieved from hash fields: " + hashes.GetSync("person-hash", "1", new string[] { "name", "age" }));
        }