1

memcachedに保存しようとしてIEnumerable<T>いますが、 のインスタンスしか正常に保存できませんでしたT

memcached に列挙を格納する別のコードはありますか?

public IEnumerable<UsContentView> GetContentViewByUserId(int userId)
    {
        Expire("contentViewUserId_" + userId);
        var result = Memcached.Get<IEnumerable<UsContentView>>("contentViewUserId_" + userId);

        if (result == null)
        {
            result = db.UsContentViews.Where(m => m.UserID == userId).OrderBy(m => m.ArticleId).Distinct();

            var arr = result.ToArray();
            var arrList = arr.ToList();
            //store it in the cache, with the key
            StoreList(arrList, "contentViewUserId_" + userId);

            MemoryStream mem = new MemoryStream();
            BinaryFormatter b = new BinaryFormatter();
            try
            {
                b.Serialize(mem, result);
            }
            catch (EntitySqlException ex)
            {
                throw new ApplicationException("The object query failed", ex);
            }
            catch (EntityCommandExecutionException ex)
            {
                throw new ApplicationException("The object query failed", ex);
            }
            catch (SerializationException ex)
            {
                throw new ApplicationException("The object graph could not be serialized", ex);
            }
        }

        return result;
    }
4

1 に答える 1

3

と同様に配列やリストに分解して格納しTます。

using System.Linq;

var array = myEnumerable.ToArray();
var list = myEnumerable.ToList();

モノのリストを使用する場合、Memcached のキー内メモリの最大値が 2MB に達しないように注意してください。キーのディスク バッキングが有効になっている場合は 10MB です。

実際には、byte[]カスタムのシリアライゼーション (自作の場合もあれば、ProtoBuf の場合もあります) を保存して使用することで、これを回避し、高速かつ軽量に保ちます。

于 2012-07-31T15:11:46.080 に答える