1+ namespace SqlStreamStore . HAL
2+ {
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+ using Microsoft . Extensions . Primitives ;
7+
8+ internal struct CacheControl : IEquatable < CacheControl >
9+ {
10+ private readonly string [ ] _values ;
11+
12+ public static readonly CacheControl NoCache = new CacheControl (
13+ "max-age=0" ,
14+ "no-cache" ,
15+ "must-revalidate" ) ;
16+
17+ public static readonly CacheControl OneYear = new CacheControl ( "max-age=31536000" ) ;
18+
19+ private CacheControl ( params string [ ] values )
20+ {
21+ if ( values == null )
22+ throw new ArgumentNullException ( nameof ( values ) ) ;
23+ _values = values ;
24+ }
25+
26+ public bool Equals ( CacheControl other ) => _values
27+ . OrderBy ( value => value )
28+ . SequenceEqual ( other . _values . OrderBy ( value => value ) , StringComparer . OrdinalIgnoreCase ) ;
29+
30+ public override bool Equals ( object obj ) => obj is CacheControl other && Equals ( other ) ;
31+
32+ public override int GetHashCode ( ) => _values
33+ . Aggregate ( 397 , ( previous , value ) => previous ^ ( value . GetHashCode ( ) * 397 ) ) ;
34+
35+ public static bool operator == ( CacheControl left , CacheControl right ) => left . Equals ( right ) ;
36+ public static bool operator != ( CacheControl left , CacheControl right ) => ! left . Equals ( right ) ;
37+ public static implicit operator string [ ] ( CacheControl cacheControl ) => cacheControl . _values ;
38+ public static implicit operator StringValues ( CacheControl cacheControl ) =>
39+ new StringValues ( cacheControl . _values ) ;
40+ public static implicit operator KeyValuePair < string , string [ ] > ( CacheControl cacheControl )
41+ => new KeyValuePair < string , string [ ] > ( Constants . Headers . CacheControl , cacheControl . _values ) ;
42+
43+ }
44+ }
0 commit comments