我正在使用 Redis 和 ServiceStack.Redis 作为客户端。我最初使用“AutoMapper”将缓存的对象映射到域对象,但这很慢。使用其他人的示例,我设置了一个自定义映射器,但这也非常慢。
下面的代码有什么明显的错误吗?从 Redis 映射 1000 个项目需要 4-5 秒。
引入滞后的是“GetByIds”客户端方法,但我想要一种有效的方法将集合存储为 Redis 中的 ID 列表,但看不到另一种将这些转换为域对象列表的方法。
谢谢!
interface IMapToNew<TSource, TTarget>
{
TTarget Map(TSource source);
}
interface IMapToExisting<TSource, TTarget>
{
void Map(TSource source, TTarget target);
}
class FullEmployeeMapper : IMapToNew<Employee, FullEmployee>
{
public FullEmployee Map(Employee source)
{
FullEmployee employee = new FullEmployee()
{
Id = source.Id,
Age = source.Age,
BirthDate = source.BirthDate,
Name = source.Name
};
var mapper = new FullRoleMapper();
var client = new RedisClient("localhost");
employee.Roles =
client
.As<Role>()
.GetByIds(source.Roles)
.Select(r => mapper.Map(r))
.ToList();
return employee;
}
}
class FullRoleMapper : IMapToNew<Role, FullRole>
{
public FullRole Map(Role source)
{
FullRole role = new FullRole()
{
Id = source.Id,
RoleName = source.RoleName
};
return role;
}
}
class FullEmployee
{
public int Id { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
public DateTime? BirthDate { get; set; }
public IList<FullRole> Roles { get; set; }
}
class FullRole
{
public int Id { get; set; }
public string RoleName { get; set; }
}
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int? Age { get; set; }
public DateTime? BirthDate { get; set; }
public IList<int> Roles { get; set; }
public Employee(int EmployeeId, string Name)
{
this.Id = EmployeeId;
this.Name = Name;
}
}
class Role
{
public int Id { get; set; }
public string RoleName { get; set; }
}
class Program
{
static void Main(string[] args)
{
var client = new RedisClient("localhost");
var employeeClient = client.As<Employee>();
var allEmployees = employeeClient.GetAll();
var allFullEmployees =
allEmployees
.Select(e => mapper.Map(e))
.ToList();
}
}