Wor*_*ker 6 erlang data-structures
我开发了我认为会有很多用户的系统.每个用户都有一个在应用程序内表示的配置文件作为记录.为了存储用户的配置文件,我执行以下操作base64:encode_to_string(term_to_binary(Profile)),因此基本上配置文件存储在序列化的maner中.
到目前为止一切都很好.现在问题是:
我不时会计划通过添加和删除其中的某些字段来扩展配置文件功能.我的问题是在代码中处理这些变化的最佳策略是什么?
我现在看到的方法是做这样的事情:
Profile = get_profile(UserName),
case is_record(Profile, #profile1) of
true ->
% do stuff with Profile#profile1
ok;
_ ->
next
end,
case is_record(Profile, #profile2) of
true ->
% do stuff with Profile#profile2
ok;
_ ->
next
end,
Run Code Online (Sandbox Code Playgroud)
我想知道我的任务是否有更好的解决方案?
附加信息:我使用的是简单的KV存储.它不能存储Erlang类型这就是我使用的原因State#state.player#player.chips#chips.br
也许,你可以使用 proplists。
假设您已经存储了一些用户配置文件。
User = [{name,"John"},{surname,"Dow"}].
store_profile(User).
Run Code Online (Sandbox Code Playgroud)
然后,几年后,您决定根据用户的年龄扩展用户配置文件。
User = [{name,"John"},{surname,"Dow"},{age,23}].
store_profile(User).
Run Code Online (Sandbox Code Playgroud)
现在您需要从数据库获取用户配置文件
get_val(Key,Profile) ->
V = lists:keyfind(Key,1,Profile),
case V of
{_,Val} -> Val;
_ -> undefined
end.
User = get_profile().
UserName = get_val(name,User).
UserAge = get_val(age,User).
Run Code Online (Sandbox Code Playgroud)
如果您获得“版本 2”的用户个人资料,您将获得实际年龄(在本例中为 23)。
如果您获得“版本 1”(“旧”版本)的用户配置文件,您将获得“未定义”的年龄, - 然后您可以更新配置文件并将其存储为新值,因此它将是“新”版本'实体。
所以,不存在版本冲突。
也许,这不是最好的方法,但在某些情况下它可能是一个解决方案。