/// <summary> /// Get given model from Redis. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns>The instance of the model if valid id, null otherwise.</returns> public static T Get <T>(long id) { var newInstance = Activator.CreateInstance <T>(); var nest = new RedisNest <T>(_database, newInstance); if (!nest.Exists(id)) { return(default(T)); } Util.SetId(newInstance, id); var fields = nest.Hgetall(id); Util.SetFields(newInstance, fields); return(newInstance); }
/// <summary> /// Save given model to Redis. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="obj"></param> /// <returns>The instance of the model supplied, with a new Id if it is newly created in redis.</returns> public static T Save <T>(T obj) { var nest = new RedisNest <T>(_database, obj); var id = Util.GetId(obj); if (id == null || id == 0) { id = nest.Incr(); Util.SetId(obj, id.Value); } if (!nest.Exists(id.Value)) { throw new CsOhmException("Attempting to save an object with an Id not generated by CsOhm"); } var fields = Util.GetFields(obj); nest.Hmset(id.Value, fields); return(obj); }