shijiang1130 发表于 2015-06-18 17:40

【erlang ets】

cache_create() ->
    ets:new(cm_tab, ).

cache_lookup(Cache, Key) ->
    case ets:lookup(Cache, Key) of
        ->
          Channel;
        [] ->
          undefined
    end.

cache_update(Cache, #channel{local_id = Id} = Entry) when Id =/= undefined ->
    ets:insert(Cache, Entry).

cache_delete(Cache, Key) ->
    ets:delete(Cache, Key).

cache_delete(Cache) ->
    ets:delete(Cache).

cache_foldl(Fun, Acc, Cache) ->
    ets:foldl(Fun, Acc, Cache).
   
cache_find(ChannelPid, Cache) ->
   case ets:match_object(Cache, #channel{user = ChannelPid}) of
       [] ->
           undefined;
       ->
           Channel
   end.

shijiang1130 发表于 2015-06-19 10:14

{keypos,Pos} Specfies which element in the stored tuples should be used as key. By default, it is the first element, i.e. Pos=1. However, this is not always appropriate. In particular, we do not want the first element to be the key if we want to store Erlang records in a table.

Note that any tuple stored in the table must have at least Pos number of elements.
页: [1]
查看完整版本: 【erlang ets】