Ejemplo n.º 1
0
        /// <summary>
        /// This method asynchronously saves a POCO to Redis stored as a JSON string using a
        /// <code>RedisKey</code> attribute at the class level
        /// </summary>
        /// <param name="redisValue"></param>
        /// <typeparam name="TValue"></typeparam>
        /// <returns></returns>
        public async Task SetAsync <TValue>(TValue redisValue)
        {
            // Define our previous database index
            int?previousDatabase = null;
            // Load the value's type
            Type type = typeof(TValue);
            // Localize the database attribute
            RedisDatabaseAttribute databaseAttribute =
                (type.GetCustomAttributes(typeof(RedisDatabaseAttribute), true).FirstOrDefault() as RedisDatabaseAttribute);
            // Localize the key attribute
            RedisKeyAttribute keyAttribute =
                (type.GetCustomAttributes(typeof(RedisKeyAttribute), true).FirstOrDefault() as RedisKeyAttribute);

            // Check for a database attribute
            if (databaseAttribute != null && databaseAttribute.Database >= 0)
            {
                // Set the previous database
                previousDatabase = DatabaseIndex;
                // Set the database index into the instance
                DatabaseIndex = databaseAttribute.Database;
            }
            // Make sure we have a key attribute
            if (keyAttribute == null)
            {
                throw new System.Exception($"{typeof(TValue).Name} Does Not Contain a RedisKey Attribute");
            }
            // Set the value into redis
            await SetAsync(keyAttribute.Name, redisValue);

            // Check to see if we stored the previous database and reset it into the instance
            if (previousDatabase.HasValue)
            {
                DatabaseIndex = previousDatabase.Value;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This method loads a POCO from Redis stored as a JSON string using a
        /// <code>RedisKey</code> attribute at the class level
        /// </summary>
        /// <typeparam name="TValue"></typeparam>
        /// <returns></returns>
        public TValue Get <TValue>()
        {
            // Define our previous database index
            int?previousDatabase = null;
            // Load the value's type
            Type type = typeof(TValue);
            // Localize the database attribute
            RedisDatabaseAttribute databaseAttribute =
                (type.GetCustomAttributes(typeof(RedisDatabaseAttribute), true).FirstOrDefault() as RedisDatabaseAttribute);
            // Localize the key attribute
            RedisKeyAttribute keyAttribute =
                (type.GetCustomAttributes(typeof(RedisKeyAttribute), true).FirstOrDefault() as RedisKeyAttribute);

            // Check for a database attribute
            if (databaseAttribute != null && databaseAttribute.Database >= 0)
            {
                // Set the previous database
                previousDatabase = DatabaseIndex;
                // Set the database index into the instance
                DatabaseIndex = databaseAttribute.Database;
            }
            // Make sure we have a key attribute
            if (keyAttribute == null)
            {
                throw new Exception($"{typeof(TValue).Name} Does Not Contain a RedisKey Attribute");
            }
            // Load the value from Redis
            TValue value = Get <TValue>(keyAttribute.Name, keyAttribute.AllowEmpty);

            // Check to see if we stored the previous database and reset it into the instance
            if (previousDatabase.HasValue)
            {
                DatabaseIndex = previousDatabase.Value;
            }
            // We're done, return the value
            return(value);
        }