Skip to content

Instantly share code, notes, and snippets.

public class UserWithPostCount
{
public int ID { get; set; }
public string Name { get; set; }
public int PostCount { get; set; }
}
public IEnumerable<UserWithPostCount> ReadTopUsers(int offset, int countCategories)
{
var cache = _muxer.GetDatabase();
public void PusblishOutstandingPosts(CancellationToken stoppingToken)
{
const string script = @"if tonumber(redis.call('ZADD', @key1, @timestamp, @value)) == 0 then
return redis.call('GET', @key2)
else
return redis.call('INCR', @key2)
end";
var prepared = LuaScript.Prepare(script);
var cache = _muxer.GetDatabase();
public IEnumerable<Category> ReadTopCategories(int offset, int countCategories)
{
var cache = _muxer.GetDatabase();
var res = cache.SortedSetRangeByRankWithScores("CategoriesByPostCount", offset, offset + countCategories -1, Order.Descending);
var ret = new List<Category>();
foreach(var e in res)
{
ret.Add(new Category { CategoryId = e.Element, PostCount = (long)e.Score });
}
return ret;
private readonly ConnectionMultiplexer _muxer = ConnectionMultiplexer.Connect("localhost:6379");
public void CreatePost(Post post)
{
using var dbContext = new PostServiceContext(GetConnectionString(post.CategoryId));
var transaction = dbContext.Database.BeginTransaction();
dbContext.Post.Add(new Post() { CategoryId = post.CategoryId, Content = post.Content, Title = post.Title, UserId = post.UserId });
dbContext.SaveChanges();
dbContext.Database.ExecuteSqlRaw("Update Category set PostCount = PostCount + 1 where CategoryId = {0}", post.CategoryId);
using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations;
namespace PostService.Entities
{
[Index(nameof(PostId), nameof(CategoryId))]
public class Post
{
public int PostId { get; set; }
if tonumber(redis.call('ZADD', KEYS[1], ARGV[1], ARGV[2])) == 1 then
return redis.call('INCR', KEYS[2])
else
return redis.call('GET', KEYS[2])
end
...
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "PostService", Version = "v1" });
});
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Linq;
using PostService.Data;
using PostService.Entities;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
version: '2'
services:
webservice:
image: 'postservice:latest'
cpus: 0.5
scale: 4
environment:
- PostDbConnectionStrings__Shard1=server=database0; port=3306; database=post; user=root; password=pw; Persist Security Info=False; Connect Timeout=300
- PostDbConnectionStrings__Shard1=server=database1; port=3306; database=post; user=root; password=pw; Persist Security Info=False; Connect Timeout=300
# - PostDbConnectionStrings__Shard2=server=database2; port=3306; database=post; user=root; password=pw; Persist Security Info=False; Connect Timeout=300
using Microsoft.AspNetCore.Mvc;
using PostService.Data;
using PostService.Entities;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PostService.Controllers
{
[Route("api/[controller]")]
[ApiController]