Skip to content

Commit e34f135

Browse files
committed
Implementation added to GIT
1 parent d5bcbe1 commit e34f135

11 files changed

+310
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using DistributedCaching.Model;
2+
using DistributedCaching.Service;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.Extensions.Configuration;
5+
using System.Collections.Generic;
6+
using System.Data.SqlClient;
7+
8+
namespace DistributedCaching.Controllers
9+
{
10+
[Route("api/caching")]
11+
[ApiController]
12+
public class CachingController : ControllerBase
13+
{
14+
15+
private IConfiguration Configuration;
16+
private readonly IMemoryCache mCache;
17+
18+
public CachingController(IConfiguration configuration, IMemoryCache memoryCache)
19+
{
20+
Configuration = configuration;
21+
mCache = memoryCache;
22+
}
23+
24+
[HttpGet("")]
25+
public ActionResult GetResult()
26+
{
27+
string cString = Configuration.GetConnectionString("SqlServerCString");
28+
29+
List<Student> student = new List<Student>();
30+
31+
student = mCache.getCache<List<Student>>("student");
32+
33+
if (student == null)
34+
{
35+
using (SqlConnection con = new SqlConnection(cString))
36+
{
37+
con.Open();
38+
39+
List<Student> studentList = new List<Student>();
40+
41+
string query = "select * from Student";
42+
43+
SqlCommand cmd = new SqlCommand(query, con);
44+
SqlDataReader sqlDataReader = cmd.ExecuteReader();
45+
46+
while (sqlDataReader.Read())
47+
{
48+
studentList.Add(new Student()
49+
{
50+
Id = (int)sqlDataReader["Id"],
51+
Name = (string)sqlDataReader["Name"],
52+
Age = (int)sqlDataReader["Age"]
53+
});
54+
}
55+
56+
mCache.setCache<List<Student>>(studentList, "student");
57+
58+
return Ok(studentList);
59+
60+
}
61+
}
62+
return Ok(student);
63+
}
64+
65+
}
66+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
9+
</ItemGroup>
10+
11+
12+
</Project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
5+
<Controller_SelectedScaffolderCategoryPath>root/Controller</Controller_SelectedScaffolderCategoryPath>
6+
</PropertyGroup>
7+
</Project>

DistributedCaching/Model/Student.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace DistributedCaching.Model
4+
{
5+
[Serializable]
6+
public class Student
7+
{
8+
public int Id { get; set; }
9+
public string Name { get; set; }
10+
public int Age { get; set; }
11+
}
12+
}

DistributedCaching/Program.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Hosting;
6+
using Microsoft.Extensions.Configuration;
7+
using Microsoft.Extensions.Hosting;
8+
using Microsoft.Extensions.Logging;
9+
10+
namespace DistributedCaching
11+
{
12+
public class Program
13+
{
14+
public static void Main(string[] args)
15+
{
16+
CreateHostBuilder(args).Build().Run();
17+
}
18+
19+
public static IHostBuilder CreateHostBuilder(string[] args) =>
20+
Host.CreateDefaultBuilder(args)
21+
.ConfigureWebHostDefaults(webBuilder =>
22+
{
23+
webBuilder.UseStartup<Startup>();
24+
});
25+
}
26+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:54486",
8+
"sslPort": 44300
9+
}
10+
},
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": false,
15+
"launchUrl": "weatherforecast",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"DistributedCaching": {
21+
"commandName": "Project",
22+
"launchBrowser": false,
23+
"launchUrl": "weatherforecast",
24+
"applicationUrl": "https://localhost:5001;http://localhost:5000",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
29+
}
30+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace DistributedCaching.Service
2+
{
3+
public interface IMemoryCache
4+
{
5+
void setCache<T>(T values, string key);
6+
T getCache<T>(string key) where T : class;
7+
void removeCache(string key);
8+
}
9+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using Microsoft.Extensions.Caching.Distributed;
2+
using System;
3+
using System.IO;
4+
using System.Runtime.Serialization.Formatters.Binary;
5+
6+
namespace DistributedCaching.Service
7+
{
8+
public class MemoryCache : IMemoryCache
9+
{
10+
public IDistributedCache _cache;
11+
public BinaryFormatter binFormatter;
12+
13+
public MemoryCache(IDistributedCache _cache)
14+
{
15+
this.binFormatter = new BinaryFormatter();
16+
this._cache = _cache;
17+
}
18+
19+
public T getCache<T>(string key) where T : class
20+
{
21+
try
22+
{
23+
byte[] values = _cache.Get(key);
24+
25+
if (values == null) return null;
26+
27+
BinaryFormatter binaryFormatter = new BinaryFormatter();
28+
using (MemoryStream memoryStream = new MemoryStream(values))
29+
{
30+
return binaryFormatter.Deserialize(memoryStream) as T;
31+
}
32+
}
33+
catch (Exception ex)
34+
{
35+
throw ex;
36+
}
37+
}
38+
39+
public void setCache<T>(T values, string key)
40+
{
41+
try
42+
{
43+
MemoryStream mStream = new MemoryStream();
44+
45+
binFormatter.Serialize(mStream, values);
46+
47+
mStream.ToArray();
48+
49+
DistributedCacheEntryOptions cacheOptions = new DistributedCacheEntryOptions()
50+
{
51+
AbsoluteExpiration = DateTime.Now.AddDays(7),
52+
SlidingExpiration = TimeSpan.FromSeconds(30)
53+
};
54+
55+
_cache.Set(key, mStream.ToArray());
56+
57+
mStream.Close();
58+
}
59+
catch (Exception ex)
60+
{
61+
throw ex;
62+
}
63+
}
64+
65+
public void removeCache(string key)
66+
{
67+
try
68+
{
69+
_cache.Remove(key);
70+
}
71+
catch (Exception ex)
72+
{
73+
throw ex;
74+
}
75+
}
76+
}
77+
}

DistributedCaching/Startup.cs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using DistributedCaching.Service;
2+
using Microsoft.AspNetCore.Builder;
3+
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.Configuration;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Hosting;
7+
8+
namespace DistributedCaching
9+
{
10+
public class Startup
11+
{
12+
public Startup(IConfiguration configuration)
13+
{
14+
Configuration = configuration;
15+
}
16+
17+
public IConfiguration Configuration { get; }
18+
19+
// This method gets called by the runtime. Use this method to add services to the container.
20+
public void ConfigureServices(IServiceCollection services)
21+
{
22+
services.AddControllers();
23+
24+
services.AddDistributedMemoryCache();
25+
26+
services.AddScoped<IMemoryCache, MemoryCache>();
27+
}
28+
29+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
30+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
31+
{
32+
if (env.IsDevelopment())
33+
{
34+
app.UseDeveloperExceptionPage();
35+
}
36+
37+
app.UseHttpsRedirection();
38+
39+
app.UseRouting();
40+
41+
app.UseAuthorization();
42+
43+
app.UseEndpoints(endpoints =>
44+
{
45+
endpoints.MapControllers();
46+
});
47+
}
48+
}
49+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
}
9+
}

DistributedCaching/appsettings.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft": "Warning",
6+
"Microsoft.Hosting.Lifetime": "Information"
7+
}
8+
},
9+
"AllowedHosts": "*",
10+
"ConnectionStrings": {
11+
"SqlServerCString": "Data Source=localhost;Initial Catalog=caching;Persist Security Info=True;User ID=sa;Password=sankhadip2017..;"
12+
}
13+
}

0 commit comments

Comments
 (0)