[ 
https://issues.apache.org/jira/browse/GEODE-9359?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17390168#comment-17390168
 ] 

ASF GitHub Bot commented on GEODE-9359:
---------------------------------------

mmartell commented on a change in pull request #834:
URL: https://github.com/apache/geode-native/pull/834#discussion_r679498413



##########
File path: netcore/NetCore.Session/NetCoreSessionState.cs
##########
@@ -0,0 +1,309 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using Apache.Geode.Client;
+using Microsoft.Extensions.Caching.Distributed;
+using Microsoft.Extensions.Logging;
+using Microsoft.Extensions.Options;
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Apache.Geode.Session {
+  public class GeodeSessionStateValue {
+    //DateTime _lastAccessTimeUtc;
+    //DateTime _expirationTimeUtc = DateTime.MinValue;
+    //TimeSpan _spanUntilStale = TimeSpan.Zero;
+    //private byte[] _value;
+
+    public GeodeSessionStateValue() {}
+    public GeodeSessionStateValue(byte[] value) {
+      FromByteArray(value);
+    }
+
+    public byte[] Value { get; set; }
+    public DateTime LastAccessTimeUtc { get; set; }
+    public DateTime ExpirationTimeUtc { get; set; } = DateTime.MinValue;
+    public TimeSpan SpanUntilStale { get; set; } = TimeSpan.Zero;
+
+    public byte[] ToByteArray() {
+      int neededBytes = 3 * sizeof(long) + Value.Length;
+      byte[] byteArray = new byte[neededBytes];
+      int byteIndex = 0;
+
+      Array.Copy(BitConverter.GetBytes(LastAccessTimeUtc.Ticks), 0, byteArray, 
byteIndex,
+                 sizeof(long));
+      byteIndex += sizeof(long);
+
+      Array.Copy(BitConverter.GetBytes(ExpirationTimeUtc.Ticks), 0, byteArray, 
byteIndex,
+                 sizeof(long));
+      byteIndex += sizeof(long);
+
+      Array.Copy(BitConverter.GetBytes(SpanUntilStale.Ticks), 0, byteArray, 
byteIndex,
+                 sizeof(long));
+      byteIndex += sizeof(long);
+
+      Array.Copy(Value, 0, byteArray, byteIndex, Value.Length);
+      return byteArray;
+    }
+
+    public void FromByteArray(byte[] data) {
+      int byteIndex = 0;
+
+      LastAccessTimeUtc = DateTime.FromBinary(BitConverter.ToInt64(data, 
byteIndex));
+      byteIndex += sizeof(long);
+
+      ExpirationTimeUtc = DateTime.FromBinary(BitConverter.ToInt64(data, 
byteIndex));
+      byteIndex += sizeof(long);
+
+      SpanUntilStale = TimeSpan.FromTicks(BitConverter.ToInt64(data, 
byteIndex));
+      byteIndex += sizeof(long);
+
+      Value = new byte[data.Length - byteIndex];
+      Array.Copy(data, byteIndex, Value, 0, data.Length - byteIndex);
+    }
+  }
+
+  public class GeodeSessionStateCache : GeodeNativeObject, IDistributedCache {
+    private readonly IGeodeCache _cache;
+    private ILogger<GeodeSessionStateCache> _logger;
+    private static Region _region;
+    private string _logLevel;
+    private string _logFile;
+    private string _regionName;
+    private readonly SemaphoreSlim _connectLock = new 
SemaphoreSlim(initialCount: 1, maxCount: 1);
+
+    public GeodeSessionStateCache(IOptions<GeodeSessionStateCacheOptions> 
optionsAccessor) {
+      var host = optionsAccessor.Value.Host;
+      var port = optionsAccessor.Value.Port;
+      _regionName = optionsAccessor.Value.RegionName;
+      _logLevel = optionsAccessor.Value.LogLevel;
+      _logFile = optionsAccessor.Value.LogFile;
+
+      _cache = CacheFactory.Create()
+                   .SetProperty("log-level", _logLevel)
+                   .SetProperty("log-file", _logFile)
+                   .CreateCache();
+
+      _cache.PoolManager.CreatePoolFactory().AddLocator(host, 
port).CreatePool("pool");
+
+      var regionFactory = _cache.CreateRegionFactory(RegionShortcut.Proxy);
+      _region = regionFactory.CreateRegion(_regionName);
+    }
+
+    // Returns the SessionStateValue for key, or null if key doesn't exist
+    public GeodeSessionStateValue GetValueForKey(string key) {
+      byte[] cacheValue = _region.GetByteArray(key);
+
+      if (cacheValue != null) {
+        return new GeodeSessionStateValue(cacheValue);
+      } else
+        return null;
+    }
+
+    public byte[] Get(string key) {
+      if (key == null) {
+        throw new ArgumentNullException(nameof(key));
+      }
+
+      Connect();
+
+      // Check for nonexistent key
+      GeodeSessionStateValue ssValue = GetValueForKey(key);
+      if (ssValue == null) {
+        return null;
+      }
+
+      // Check for expired key
+      DateTime nowUtc = DateTime.UtcNow;
+      if (ssValue.ExpirationTimeUtc != DateTime.MinValue && 
ssValue.ExpirationTimeUtc < nowUtc) {
+        return null;
+      }
+
+      // Check for stale key
+      if (ssValue.SpanUntilStale != TimeSpan.Zero &&
+          nowUtc > (ssValue.LastAccessTimeUtc + ssValue.SpanUntilStale)) {
+        return null;
+      }
+
+      // Update the times for sliding expirations
+      if (ssValue.SpanUntilStale != TimeSpan.Zero) {
+        ssValue.LastAccessTimeUtc = nowUtc;
+        _region.PutByteArray(key, ssValue.ToByteArray());
+      }
+
+      return ssValue.Value;
+    }
+
+    public Task<byte[]> GetAsync(string key, CancellationToken token = 
default(CancellationToken)) {
+      if (key == null) {
+        throw new ArgumentNullException(nameof(key));
+      }
+
+      token.ThrowIfCancellationRequested();
+
+      return Task.Factory.StartNew(() => Get(key), token);
+    }
+
+    public void Refresh(string key) {
+      if (key == null) {
+        throw new ArgumentNullException(nameof(key));
+      }
+
+      Connect();
+
+      // Check for nonexistent key
+      GeodeSessionStateValue ssValue = GetValueForKey(key);
+      if (ssValue == null) {
+        return;
+      }
+
+      // Check for expired key
+      var nowUtc = DateTime.UtcNow;
+      if (ssValue.ExpirationTimeUtc != DateTime.MinValue && 
ssValue.ExpirationTimeUtc < nowUtc) {
+        return;
+      }
+
+      // Check for stale key
+      if (ssValue.SpanUntilStale != TimeSpan.Zero &&
+          nowUtc > (ssValue.LastAccessTimeUtc + ssValue.SpanUntilStale)) {
+        return;
+      }
+
+      // Update the times for sliding expirations
+      if (ssValue.SpanUntilStale != TimeSpan.Zero) {
+        ssValue.LastAccessTimeUtc = nowUtc;
+        _region.PutByteArray(key, ssValue.ToByteArray());
+      }
+    }
+
+    public Task RefreshAsync(string key, CancellationToken token = 
default(CancellationToken)) {
+      if (key == null) {
+        throw new ArgumentNullException(nameof(key));
+      }
+
+      token.ThrowIfCancellationRequested();
+
+      return Task.Factory.StartNew(() => Refresh(key), token);
+    }
+
+    public void Remove(string key) {
+      if (key == null) {
+        throw new ArgumentNullException(nameof(key));
+      }
+
+      Connect();
+
+      // Until we return error codes

Review comment:
       Yup.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@geode.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> add net-core-session to geode-native
> ------------------------------------
>
>                 Key: GEODE-9359
>                 URL: https://issues.apache.org/jira/browse/GEODE-9359
>             Project: Geode
>          Issue Type: New Feature
>          Components: native client
>            Reporter: Ernest Burghardt
>            Priority: Major
>              Labels: pull-request-available
>
> net-core-session shall be added to the top level of geode-native repo and 
> will produce a separate binary that will be publishable to NuGet
> https://docs.microsoft.com/en-us/nuget/create-packages/package-authoring-best-practices



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to