从Redis映射数据的有效方法

时间:2014-09-12 18:23:30

标签: c# redis automapper servicestack.redis

我正在玩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();
    }
}

2 个答案:

答案 0 :(得分:1)

Automapper使用可能很慢的反射。

查看EmitMapper的效果。

答案 1 :(得分:1)

您可以使用延迟加载,以便仅在需要时加载Roles集合。这是通过在FullEmployee实体中注入角色存储库来完成的。

您还可以为所有人加载一次角色:在FullEmployeeMapper中保留角色词典并在加载时填写它,然后在查询缓存之前检查它。希望您为每个工作单元重新创建一个实例,这样每个新工作的字典都会很新,并且您可以避免多线程问题。

使用List的示例实现:

class FullEmployeeMapper : IMapToNew<Employee, FullEmployee>
{
    private List<FullRole> _roles = new List<FullRole>();
    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 = _roles.Where(r => source.Roles.Contains(r.Id)).ToList();
        if (employee.Roles.Count != source.Roles.Count)
        {
            var newRoles = client
                .As<Role>()
                .GetByIds(source.Roles.Except(employee.Roles.Select(r => r.Id)))
                .Select(r => mapper.Map(r)))
                .ToList();
            employee.Roles.AddRange(newRoles);
            _roles.AddRange(newRoles);
        }
        return employee;
    }
}