0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

StackExchange.Redisで閲覧履歴を実装

Last updated at Posted at 2017-10-19

開発メモ

StackExchange.Redisの使い方は下記参照
https://github.com/StackExchange/StackExchange.Redis

#登録

        //RedisのDBを選択
        IDatabase db = RedisRepository.SelectCache(0);

        //キーを生成
        var key = "hoge";

        // スコア算出の為、Unixtimeを取得
        var unixSeconds = DateTimeOffset.Now.ToUnixTimeSeconds();
        // 経過秒数をスコアとして保存
        var score = (double)unixSeconds;

        //閲覧履歴の情報をソート済みセット型で登録
        db.SortedSetAdd(key, "りんご", score);

        //保持期間を設定する場合は下記(例:30日)
        db.KeyExpire(key, new TimeSpan(30, 0, 0, 0));

        //最大件数を設定する場合は下記
        var current = db.SortedSetRangeByScore(key);
        var maxCnt = 10;
        if (current.Count() > maxCnt)
        {
            //最下位のデータを削除
            db.SortedSetRemoveRangeByRank(key,0,0);
        }

#取得

        //最新の閲覧順(スコアが高い順)に取得
        var list = db.SortedSetRangeByScore(key, order: Order.Descending);
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?