Hatena::Grouperlang

檜山正幸のErlang未確認情報 RSSフィード

 | 

2009-04-08 (水)

乱数からIDを作る

| 13:42

YAWSソースから抜き出したサンプル。

%% -*- coding: utf-8 -*-

%% @doc 乱数に基づく「ほぼID」の生成
-module(randomid).
-export([init/0, gen/0]).

%% spec () -> {integer(), integer(), integer()}
seed() ->
  now(). % {X, Y, Z}

%% @spec () -> ok
init() ->
  {X,Y,Z} = seed(),
  random:seed(X, Y, Z),
  ok.

%% @spec () -> string()
gen() ->
  N = random:uniform(16#ffffffffffffffff), %% 64 bits
  integer_to_list(N).

YAWSのクッキーセッション

| 13:47

yaws_apiにあるいくつかの関数を使う。説明をそのうち書くだろう、たぶん。

-record(myopaque, {udata,
                   times = 0,
                   foobar}).

out(A) ->
    H = A#arg.headers,
    C = H#headers.cookie,
    case yaws_api:find_cookie_val("baz", C) of
        [] ->
            M = #myopaque{},
            Cookie = yaws_api:new_cookie_session(M),
            Data = {ehtml,
                    {html,[],
                     ["I just set your cookie to ", Cookie, "Click ",
                      {a, [{href,"session1.yaws"}], " here "},
                      "to revisit"]}},
            CO = yaws_api:setcookie("baz",Cookie,"/"),
            [Data, CO];
        Cookie ->
            {ok, OP} = yaws_api:cookieval_to_opaque(Cookie),
            OP2 = OP#myopaque{times = OP#myopaque.times + 1},
            yaws_api:replace_cookie_session(Cookie, OP2),
            Data = {ehtml,
                    {html,[],
                     [
                      "Click ",
                      {a, [{href,"session1.yaws"}], " here "},
                      "to revisit",
                      {p, [], f("You have been here ~p times", [OP2#myopaque.times])},
                      {p, [], f("Your cookie is ~s", [Cookie])}]}},
            Data
    end.

ゲスト



トラックバック - http://erlang.g.hatena.ne.jp/m-hiyama/20090408
 |