欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Asp.net Core 使用Redis存储Session

程序员文章站 2022-06-09 09:25:26
前言 asp.net core 改变了之前的封闭,现在开源且开放,下面我们来用redis存储session来做一个简单的测试,或者叫做中间件(middleware)。...

前言

asp.net core 改变了之前的封闭,现在开源且开放,下面我们来用redis存储session来做一个简单的测试,或者叫做中间件(middleware)。

对于session来说褒贬不一,很多人直接说不要用,也有很多人在用,这个也没有绝对的这义,个人认为只要不影什么且又可以方便实现的东西是可以用的,现在不对可不可用做表态,我们只关心实现。

类库引用

这个相对于之前的.net是方便了不少,需要在project.json中的dependencies节点中添加如下内容:

  "stackexchange.redis": "1.1.604-alpha",
  "microsoft.aspnetcore.session": "1.1.0-alpha1-21694"

redis实现

这里并非我实现,而是借用不知道为什么之前还有这个类库,而现在nuget止没有了,为了不影响日后升级我的命名空间也用 microsoft.extensions.caching.redis

可以看到微软这里有四个类,其实我们只需要三个,第四个拿过来反而会出错:

using system;
using system.threading.tasks;
using microsoft.extensions.caching.distributed;
using microsoft.extensions.options;
using stackexchange.redis;

namespace microsoft.extensions.caching.redis
{
  public class rediscache : idistributedcache, idisposable
  {
    // keys[1] = = key
    // argv[1] = absolute-expiration - ticks as long (-1 for none)
    // argv[2] = sliding-expiration - ticks as long (-1 for none)
    // argv[3] = relative-expiration (long, in seconds, -1 for none) - min(absolute-expiration - now, sliding-expiration)
    // argv[4] = data - byte[]
    // this order should not change lua script depends on it
    private const string setscript = (@"
        redis.call('hmset', keys[1], 'absexp', argv[1], 'sldexp', argv[2], 'data', argv[4])
        if argv[3] ~= '-1' then
         redis.call('expire', keys[1], argv[3])
        end
        return 1");
    private const string absoluteexpirationkey = "absexp";
    private const string slidingexpirationkey = "sldexp";
    private const string datakey = "data";
    private const long notpresent = -1;

    private connectionmultiplexer _connection;
    private idatabase _cache;

    private readonly rediscacheoptions _options;
    private readonly string _instance;

    public rediscache(ioptions<rediscacheoptions> optionsaccessor)
    {
      if (optionsaccessor == null)
      {
        throw new argumentnullexception(nameof(optionsaccessor));
      }

      _options = optionsaccessor.value;

      // this allows partitioning a single backend cache for use with multiple apps/services.
      _instance = _options.instancename ?? string.empty;
    }

    public byte[] get(string key)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      return getandrefresh(key, getdata: true);
    }

    public async task<byte[]> getasync(string key)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      return await getandrefreshasync(key, getdata: true);
    }

    public void set(string key, byte[] value, distributedcacheentryoptions options)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      if (value == null)
      {
        throw new argumentnullexception(nameof(value));
      }

      if (options == null)
      {
        throw new argumentnullexception(nameof(options));
      }

      connect();

      var creationtime = datetimeoffset.utcnow;

      var absoluteexpiration = getabsoluteexpiration(creationtime, options);

      var result = _cache.scriptevaluate(setscript, new rediskey[] { _instance + key },
        new redisvalue[]
        {
            absoluteexpiration?.ticks ?? notpresent,
            options.slidingexpiration?.ticks ?? notpresent,
            getexpirationinseconds(creationtime, absoluteexpiration, options) ?? notpresent,
            value
        });
    }

    public async task setasync(string key, byte[] value, distributedcacheentryoptions options)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      if (value == null)
      {
        throw new argumentnullexception(nameof(value));
      }

      if (options == null)
      {
        throw new argumentnullexception(nameof(options));
      }

      await connectasync();

      var creationtime = datetimeoffset.utcnow;

      var absoluteexpiration = getabsoluteexpiration(creationtime, options);

      await _cache.scriptevaluateasync(setscript, new rediskey[] { _instance + key },
        new redisvalue[]
        {
            absoluteexpiration?.ticks ?? notpresent,
            options.slidingexpiration?.ticks ?? notpresent,
            getexpirationinseconds(creationtime, absoluteexpiration, options) ?? notpresent,
            value
        });
    }

    public void refresh(string key)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      getandrefresh(key, getdata: false);
    }

    public async task refreshasync(string key)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      await getandrefreshasync(key, getdata: false);
    }

    private void connect()
    {
      if (_connection == null)
      {
        _connection = connectionmultiplexer.connect(_options.configuration);
        _cache = _connection.getdatabase();
      }
    }

    private async task connectasync()
    {
      if (_connection == null)
      {
        _connection = await connectionmultiplexer.connectasync(_options.configuration);
        _cache = _connection.getdatabase();
      }
    }

    private byte[] getandrefresh(string key, bool getdata)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      connect();

      // this also resets the lru status as desired.
      // todo: can this be done in one operation on the server side? probably, the trick would just be the datetimeoffset math.
      redisvalue[] results;
      if (getdata)
      {
        results = _cache.hashmemberget(_instance + key, absoluteexpirationkey, slidingexpirationkey, datakey);
      }
      else
      {
        results = _cache.hashmemberget(_instance + key, absoluteexpirationkey, slidingexpirationkey);
      }

      // todo: error handling
      if (results.length >= 2)
      {
        // note we always get back two results, even if they are all null.
        // these operations will no-op in the null scenario.
        datetimeoffset? absexpr;
        timespan? sldexpr;
        mapmetadata(results, out absexpr, out sldexpr);
        refresh(key, absexpr, sldexpr);
      }

      if (results.length >= 3 && results[2].hasvalue)
      {
        return results[2];
      }

      return null;
    }

    private async task<byte[]> getandrefreshasync(string key, bool getdata)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      await connectasync();

      // this also resets the lru status as desired.
      // todo: can this be done in one operation on the server side? probably, the trick would just be the datetimeoffset math.
      redisvalue[] results;
      if (getdata)
      {
        results = await _cache.hashmembergetasync(_instance + key, absoluteexpirationkey, slidingexpirationkey, datakey);
      }
      else
      {
        results = await _cache.hashmembergetasync(_instance + key, absoluteexpirationkey, slidingexpirationkey);
      }

      // todo: error handling
      if (results.length >= 2)
      {
        // note we always get back two results, even if they are all null.
        // these operations will no-op in the null scenario.
        datetimeoffset? absexpr;
        timespan? sldexpr;
        mapmetadata(results, out absexpr, out sldexpr);
        await refreshasync(key, absexpr, sldexpr);
      }

      if (results.length >= 3 && results[2].hasvalue)
      {
        return results[2];
      }

      return null;
    }

    public void remove(string key)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      connect();

      _cache.keydelete(_instance + key);
      // todo: error handling
    }

    public async task removeasync(string key)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      await connectasync();

      await _cache.keydeleteasync(_instance + key);
      // todo: error handling
    }

    private void mapmetadata(redisvalue[] results, out datetimeoffset? absoluteexpiration, out timespan? slidingexpiration)
    {
      absoluteexpiration = null;
      slidingexpiration = null;
      var absoluteexpirationticks = (long?)results[0];
      if (absoluteexpirationticks.hasvalue && absoluteexpirationticks.value != notpresent)
      {
        absoluteexpiration = new datetimeoffset(absoluteexpirationticks.value, timespan.zero);
      }
      var slidingexpirationticks = (long?)results[1];
      if (slidingexpirationticks.hasvalue && slidingexpirationticks.value != notpresent)
      {
        slidingexpiration = new timespan(slidingexpirationticks.value);
      }
    }

    private void refresh(string key, datetimeoffset? absexpr, timespan? sldexpr)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      // note refresh has no effect if there is just an absolute expiration (or neither).
      timespan? expr = null;
      if (sldexpr.hasvalue)
      {
        if (absexpr.hasvalue)
        {
          var relexpr = absexpr.value - datetimeoffset.now;
          expr = relexpr <= sldexpr.value ? relexpr : sldexpr;
        }
        else
        {
          expr = sldexpr;
        }
        _cache.keyexpire(_instance + key, expr);
        // todo: error handling
      }
    }

    private async task refreshasync(string key, datetimeoffset? absexpr, timespan? sldexpr)
    {
      if (key == null)
      {
        throw new argumentnullexception(nameof(key));
      }

      // note refresh has no effect if there is just an absolute expiration (or neither).
      timespan? expr = null;
      if (sldexpr.hasvalue)
      {
        if (absexpr.hasvalue)
        {
          var relexpr = absexpr.value - datetimeoffset.now;
          expr = relexpr <= sldexpr.value ? relexpr : sldexpr;
        }
        else
        {
          expr = sldexpr;
        }
        await _cache.keyexpireasync(_instance + key, expr);
        // todo: error handling
      }
    }

    private static long? getexpirationinseconds(datetimeoffset creationtime, datetimeoffset? absoluteexpiration, distributedcacheentryoptions options)
    {
      if (absoluteexpiration.hasvalue && options.slidingexpiration.hasvalue)
      {
        return (long)math.min(
          (absoluteexpiration.value - creationtime).totalseconds,
          options.slidingexpiration.value.totalseconds);
      }
      else if (absoluteexpiration.hasvalue)
      {
        return (long)(absoluteexpiration.value - creationtime).totalseconds;
      }
      else if (options.slidingexpiration.hasvalue)
      {
        return (long)options.slidingexpiration.value.totalseconds;
      }
      return null;
    }

    private static datetimeoffset? getabsoluteexpiration(datetimeoffset creationtime, distributedcacheentryoptions options)
    {
      if (options.absoluteexpiration.hasvalue && options.absoluteexpiration <= creationtime)
      {
        throw new argumentoutofrangeexception(
          nameof(distributedcacheentryoptions.absoluteexpiration),
          options.absoluteexpiration.value,
          "the absolute expiration value must be in the future.");
      }
      var absoluteexpiration = options.absoluteexpiration;
      if (options.absoluteexpirationrelativetonow.hasvalue)
      {
        absoluteexpiration = creationtime + options.absoluteexpirationrelativetonow;
      }

      return absoluteexpiration;
    }

    public void dispose()
    {
      if (_connection != null)
      {
        _connection.close();
      }
    }
  }
}

using microsoft.extensions.options;

namespace microsoft.extensions.caching.redis
{
  /// <summary>
  /// configuration options for <see cref="rediscache"/>.
  /// </summary>
  public class rediscacheoptions : ioptions<rediscacheoptions>
  {
    /// <summary>
    /// the configuration used to connect to redis.
    /// </summary>
    public string configuration { get; set; }

    /// <summary>
    /// the redis instance name.
    /// </summary>
    public string instancename { get; set; }

    rediscacheoptions ioptions<rediscacheoptions>.value
    {
      get { return this; }
    }
  }
}

using system.threading.tasks;
using stackexchange.redis;

namespace microsoft.extensions.caching.redis
{
  internal static class redisextensions
  {
    private const string hmgetscript = (@"return redis.call('hmget', keys[1], unpack(argv))");

    internal static redisvalue[] hashmemberget(this idatabase cache, string key, params string[] members)
    {
      var result = cache.scriptevaluate(
        hmgetscript,
        new rediskey[] { key },
        getredismembers(members));

      // todo: error checking?
      return (redisvalue[])result;
    }

    internal static async task<redisvalue[]> hashmembergetasync(
      this idatabase cache,
      string key,
      params string[] members)
    {
      var result = await cache.scriptevaluateasync(
        hmgetscript,
        new rediskey[] { key },
        getredismembers(members));

      // todo: error checking?
      return (redisvalue[])result;
    }

    private static redisvalue[] getredismembers(params string[] members)
    {
      var redismembers = new redisvalue[members.length];
      for (int i = 0; i < members.length; i++)
      {
        redismembers[i] = (redisvalue)members[i];
      }

      return redismembers;
    }
  }
}

配置启用session

我们在startup中configureservices增加

services.addsingleton<idistributedcache>(
        serviceprovider =>
          new rediscache(new rediscacheoptions
          {
            configuration = "192.168.178.141:6379",
            instancename = "sample:"
          }));
      services.addsession();

在startup中configure增加

app.usesession(new sessionoptions() { idletimeout = timespan.fromminutes(30) });

到此我们的配置完毕,可以测试一下是否写到了redis中

验证结果

在mvc项目中,我们来实现如下代码

if (string.isnullorempty(httpcontext.session.getstring("d")))
      {
        var d = datetime.now.tostring();
        httpcontext.session.setstring("d", d);
        httpcontext.response.contenttype = "text/plain";
        await httpcontext.response.writeasync("hello first timer///" + d);
      }
      else
      {
        httpcontext.response.contenttype = "text/plain";
        await httpcontext.response.writeasync("hello old timer///" + httpcontext.session.getstring("d"));
      }

运行我们发现第一次出现了hello first timer字样,刷新后出现了hello old timer字样,证明session成功,再查看一下redis看一下,有值了,这样一个分布式的session就成功实现了。

对于上面的实例我把源码放在了:demo下载

tianwei.microsoft.extensions.caching.redis ,只是id加了tianwei 空间名还是microsoft.extensions.caching.redis

从上面的实例我们发现微软这次是真的开放了,这也意味着如果我们使用某些类不顺手或不合适时可以自已写自已扩展

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。