小编Tom*_*ane的帖子

为什么散列函数左移 5 位?

在 JavaScript 的简单(非安全)散列函数中给出了在 JS 中生成散列函数的流行答案在Javascript中从字符串生成哈希

代码示例之一是:

String.prototype.hashCode = function() {
    var hash = 0;
    if (this.length == 0) {
        return hash;
    }
    for (var i = 0; i < this.length; i++) {
        var char = this.charCodeAt(i);
        hash = ((hash<<5)-hash)+char;
        hash = hash & hash; // Convert to 32bit integer
    }
    return hash;
}
Run Code Online (Sandbox Code Playgroud)

对我来说没有意义的一行是 hash = ((hash<<5)-hash)+char;

有人可以解释为什么要这样做吗?我认为我们正在5 bit left shift对哈希进行处理。有什么理由为什么它是 5 位而不是 4 位或 6 位吗?另外为什么我们然后减去散列并添加字符?

javascript hash bitwise-operators

5
推荐指数
1
解决办法
1832
查看次数

nginx.conf中的expires会在ubuntu上引发错误,但在OSX上不会引发错误

我的nginx.conf正在使用本地(OSX),但在prod(Ubuntu)上抛出错误

完整文件:https://github.com/thomasdane/partywave/blob/master/nginx.conf

但相关部分是:

# Expires map
map $sent_http_content_type $expires {
    default                    off;
    text/html                  1w;
    text/css                   1w;
    application/javascript     1w;
    ~image/                    1w;
}

server {
    listen       80;
    expires $expires;
    server_name  http://www.partywave.co/;
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的Mac上,这很好用,输出:

curl -I localhost/images/hero.jpg
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Tue, 25 Oct 2016 09:27:51 GMT
Content-Type: image/jpeg
Content-Length: 270624
Connection: keep-alive
X-Powered-By: Express
Accept-Ranges: bytes
Cache-Control: max-age=604800 …
Run Code Online (Sandbox Code Playgroud)

ubuntu nginx

4
推荐指数
1
解决办法
1303
查看次数

为什么httpJSON失败,但httpLBS成功了?

此功能(使用httpLBS)有效:

makeRequest = do
  response <- httpLBS "http://httpbin.org/get" 
  putStrLn $ "The status code was: " ++ show (getResponseStatusCode response)
Run Code Online (Sandbox Code Playgroud)

但是这个函数(使用httpJSON)不会:

makeRequest = do
  response <- httpJSON "http://httpbin.org/get" 
  putStrLn $ "The status code was: " ++ show (getResponseStatusCode response)
Run Code Online (Sandbox Code Playgroud)

它抛出错误:

Ambiguous type variable `a0' arising from a use of `httpJSON' prevents the constraint 
`(aeson-1.1.2.0:Data.Aeson.Types.FromJSON.FromJSON a0)' from being solved.
          Probable fix: use a type annotation to specify what `a0' should be.
Run Code Online (Sandbox Code Playgroud)

haskell http-conduit

4
推荐指数
1
解决办法
153
查看次数

在.NET中,为什么ObjectCache比MemoryCache是​​首选类型?

所以 很多 的例子使用.NET中的内存缓存(的包括官方文档)与实例吧:

private readonly ObjectCache memoryCache = MemoryCache.Default;
Run Code Online (Sandbox Code Playgroud)

有什么理由要优先于此:

private readonly MemoryCache memoryCache = MemoryCache.Default;
Run Code Online (Sandbox Code Playgroud)

.net c# objectcache memorycache

1
推荐指数
2
解决办法
218
查看次数