从Redis映射数据的有效方法 [英] Efficient way of mapping data from Redis

查看:314
本文介绍了从Redis映射数据的有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Redis和ServiceStack.Redis作为客户端.我最初使用"AutoMapper"将缓存的对象映射到域对象中,但这非常慢.以其他人的示例为例,我设置了一个自定义映射器,但这也确实很慢.

I'm playing around with Redis and with ServiceStack.Redis as a client. I initially used 'AutoMapper' to map the cached objects into domain objects, but this was pretty slow. Using someone else's example, I set up a custom mapper but this, too, is really slow.

以下代码存在明显错误吗?从Redis映射1000个项目需要4-5秒.

Is there something glaringly wrong with the below code? It's taking 4-5 seconds to map 1000 items from Redis.

这是引入滞后的"GetByIds"客户端方法,但是我想要一种有效的方式来存储集合,因为Redis中的ID列表看不到将其转换为域对象列表的另一种方法.

It's the 'GetByIds' client method that's introducing the lag, but I want an efficient way to store collections as lists of IDs in Redis don't see another way to convert these to lists of domain objects.

谢谢!

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();
    }
}

推荐答案

Automapper使用反射速度可能很慢.

Automapper uses reflection which may be slow.

查看 EmitMapper 以获得性能.

这篇关于从Redis映射数据的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆